Skip to content

JavaScript 示例

Node.js 集成

基础使用

const { PInstaller } = require('pinstaller');

// 创建实例
const installer = new PInstaller();

// 安装包
installer.install('axios')
  .then(() => console.log('安装成功'))
  .catch(err => console.error('安装失败:', err));

配置管理

// 配置选项
const config = {
  targetDir: '/usr/local/bin',
  backupExisting: true,
  logLevel: 'info'
};

const installer = new PInstaller(config);

前端集成

浏览器环境

// 动态加载
import('pinstaller').then(module => {
  const installer = new module.PInstaller();
  return installer.install('lodash');
});

错误处理

```javascript try { await installer.install('invalid-package'); } catch (error) { if (error.code === 'PACKAGE_NOT_FOUND') { console.error('包不存在'); } else if (error.code === 'NETWORK_ERROR') { console.error('网络错误'); } }