You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用pkg编译Node.js项目为exe后,如何识别新增的/public/bots目录下JS文件?

用Pkg编译Node.js为Exe后动态加载外部JS文件的实现方案

需求可行性

完全可以实现。你遇到的问题根源在于:原代码依赖__dirname指向pkg编译后的虚拟内部目录,且require动态加载的文件不会被pkg打包,导致运行时无法识别外部新增的文件。

解决步骤及代码示例

1. 定位外部实际目录

放弃使用__dirname,改用process.execPath获取exe的实际路径,再拼接外部的public/bots目录,确保指向exe同级的真实文件夹。

2. 用VM模块动态加载外部JS文件

避免使用require(pkg对外部文件的require支持有限),改用fs读取文件内容,配合vm模块模拟Node.js模块环境执行代码,从而获取导出的函数。

修改后的完整代码:

const path = require('path');
const fs = require('fs');
const vm = require('vm');

// 获取exe所在的实际目录
const exeDir = path.dirname(process.execPath);
// 拼接外部的public/bots路径
const directoryPath = path.join(exeDir, 'public', 'bots');

// 确保目录存在,不存在则递归创建
if (!fs.existsSync(directoryPath)) {
  fs.mkdirSync(directoryPath, { recursive: true });
}

fs.readdir(directoryPath, (err, files) => {
  if (err) {
    console.error('读取目录错误:', err);
    return;
  }

  console.log('文件列表:');
  files.forEach(file => {
    // 只处理.js后缀的文件
    if (path.extname(file) !== '.js') return;
    
    const filePath = path.join(directoryPath, file);
    console.log(filePath);

    try {
      // 读取外部JS文件内容
      const fileContent = fs.readFileSync(filePath, 'utf8');
      
      // 模拟Node.js模块上下文,让外部文件可以正常使用module/exports
      const moduleContext = {
        module: {},
        exports: {},
        require: require,
        __dirname: directoryPath,
        __filename: filePath
      };

      // 在新上下文中执行代码
      vm.runInNewContext(fileContent, moduleContext, {
        filename: filePath, // 便于错误定位
        displayErrors: true
      });

      // 获取模块导出的内容
      const botModule = moduleContext.module.exports || moduleContext.exports;
      
      // 这里可以调用你需要的特定函数,比如假设每个bot都有run方法
      if (typeof botModule.run === 'function') {
        console.log(`成功加载bot文件 ${file},可执行其run方法`);
        // botModule.run(); // 按需调用
      }
    } catch (e) {
      console.error(`加载文件 ${file} 失败:`, e);
    }
  });
});

编译注意事项

  • 编译时pkg会提示Cannot resolve 'filePath'的警告,直接忽略即可——因为我们现在加载的是外部文件,不需要pkg提前打包这些未知文件。
  • 若要消除警告,可在项目的package.json中添加pkg配置,明确不需要打包动态脚本:
{
  "pkg": {
    "scripts": [],
    "targets": ["node18-win-x64"] // 根据你的Node版本和系统调整
  }
}

运行说明

编译生成exe后,在exe的同级目录创建public/bots文件夹,将新增的.js文件放入其中,启动exe即可检测到这些文件并导入指定函数。

内容的提问来源于stack exchange,提问作者npc-100lvl

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.13 02:43:28