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

问题描述 投票:0回答:1

我在一个项目中以编程方式使用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 }

我试过在npm install node-gyp里面做node_modules/npm,也在我自己的项目里面让Pkg把它包含在二进制文件中。

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

任何帮助/替代品将不胜感激。

node.js npm npm-install binaryfiles node-gyp
1个回答
1
投票

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

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

© www.soinside.com 2019 - 2024. All rights reserved.