使用 Pkg 将代码编译为二进制文件时,以编程方式使用 NPM 安装的问题

Issue with using NPM install programmatically when the code is compiled into binary using Pkg

我在最终编译为二进制的项目中以编程方式使用 NPM。在 运行 二进制文件中使用 npm.commands.install 时抛出以下错误:

[2018/09/08 19:55:59:0503] LOG: POST -> /processor/mutator/install
{ Error: Cannot find module 'node-gyp/bin/node-gyp'
1) If you want to compile the package/file into executable, please pay attention to compilation warnings and specify a literal in 'require' call. 2) If you don't want to compile the package/file into executable and want to 'require' it from filesystem (likely plugin), specify an absolute path in 'require' call using process.cwd() or process.execPath.
    at Function.Module._resolveFilename (module.js:545:15)
    at Function.Module._resolveFilename (pkg/prelude/bootstrap.js:1278:46)
    at resolveFileName (/snapshot/chisel/node_modules/npm/node_modules/npm-lifecycle/node_modules/resolve-from/index.js:29:39)
    at resolveFrom (/snapshot/chisel/node_modules/npm/node_modules/npm-lifecycle/node_modules/resolve-from/index.js:43:9)
    at module.exports (/snapshot/chisel/node_modules/npm/node_modules/npm-lifecycle/node_modules/resolve-from/index.js:46:41)
    at Object.<anonymous> (/snapshot/chisel/node_modules/npm/node_modules/npm-lifecycle/index.js:18:31)
    at Module._compile (pkg/prelude/bootstrap.js:1252:22)
    at Object.Module._extensions..js (module.js:661:10)
    at Module.load (module.js:563:32)
    at tryModuleLoad (module.js:503:12)
    at Function.Module._load (module.js:495:3)
    at Module.require (module.js:594:17)
    at Module.require (pkg/prelude/bootstrap.js:1157:31)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (/snapshot/chisel/node_modules/npm/lib/utils/lifecycle.js:4:19)
    at Module._compile (pkg/prelude/bootstrap.js:1252:22) code: 'MODULE_NOT_FOUND', pkg: true }

我已经尝试在 node_modules/npm 和我自己的项目中执行 npm install node-gyp 以将 Pkg 包含在二进制文件中。

不确定是否可以切换到 child_process 而 Node.js 和 NPM 打包在二进制文件中(我试图使二进制文件独立于 Node.js 和 NPM ).

任何 help/alternatives 将不胜感激。

pkg 编译器将所有 node_module 脚本捆绑到编译后的二进制文件中。但是,当使用某些外部二进制文件(如 node-gyp 可执行文件(在 node_modules/node-gyp/bin/node-gyp 内)、phanthom js 等)的依赖项之一时,pkg 无法进行编译。如果要使用 pkg 模块编译代码,则必须避免这些依赖项。

否则您可以重构 node-gyp 模块并将可执行文件与编译后的二进制文件分开,这可能是一种高级替代方法并且很耗时。