Search code examples
node.jsnpmnpm-installbinaryfilesnode-gyp

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


I'm using NPM programmatically in a project which is compiled to binary in the end. Upon using npm.commands.install when running the binary file the following error is thrown:

[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 }

I've tried doing npm install node-gyp inside node_modules/npm and also inside my own project to have Pkg include it inside the binary.

Not sure if switching to child_process is possible while Node.js and NPM are packed inside the binary file (I'm trying to keep the binary file independent of Node.js and NPM).

Any help/alternatives would be appreciated.


Solution

  • pkg compiler bundles all your node_module script into the compiled binary. But when, one of dependencies using some external binaries like node-gyp executable (inside node_modules/node-gyp/bin/node-gyp), phanthom js and etc, the pkg cannot do the compilation. You will have to avoid those dependencies if you want to compile your code with pkg module.

    Otherwise you can refactor the node-gyp module and separate the executable them ship it with your compiled binary, which could be an advanced alternative and time consuming.