1

Can node.js be configured to only use a globally-installed modules and ignore a local node_modules directory?

For our node.js app (currently node.js 8.10.0), we build a Docker image where the required modules are installed globally, in a /node_modules folder, with the environment set to NODE_PATH=/node_modules. This works for production deployment.

For local development, we run the same Docker image, but mount the local development files at /app. This allows any locally changed files to be used by the image.

This works, until a developer runs npm install on their host system, and creates a node_modules directory. This is mounted at /app/node_modules. node examines this folder on startup, and starts complaining about missing binaries:

kumascript_1     | ## There is an issue with `node-fibers` ##
kumascript_1     | `/app/node_modules/fibers/bin/linux-x64-57/fibers.node` is missing.
kumascript_1     | 
kumascript_1     | Try running this to fix the issue: /usr/local/bin/node /app/node_modules/fibers/build
kumascript_1     | /app/node_modules/fibers/fibers.js:20
kumascript_1     |  throw new Error('Missing binary. See message above.');
kumascript_1     |  ^
kumascript_1     | 
kumascript_1     | Error: Missing binary. See message above.
kumascript_1     |     at Object.<anonymous> (/app/node_modules/fibers/fibers.js:20:8)
kumascript_1     |     at Module._compile (module.js:652:30)
kumascript_1     |     at Object.Module._extensions..js (module.js:663:10)
kumascript_1     |     at Module.load (module.js:565:32)
kumascript_1     |     at tryModuleLoad (module.js:505:12)
kumascript_1     |     at Function.Module._load (module.js:497:3)
kumascript_1     |     at Module.require (module.js:596:17)
kumascript_1     |     at require (internal/module.js:11:18)
kumascript_1     |     at Object.<anonymous> (/app/lib/kumascript/templates.js:11:13)
kumascript_1     |     at Module._compile (module.js:652:30)

If there was a way to tell node to ignore /app/node_modules, then a developer can do what they want with npm install on the host system, and the Docker container would continue running as expected.

jwhitlock
  • 4,572
  • 4
  • 39
  • 49
  • [this may help you](https://stackoverflow.com/questions/7970793/how-do-i-import-global-modules-in-node-i-get-error-cannot-find-module-module) – Sravan Apr 05 '18 at 13:30

0 Answers0