node – how to fix cannot find module error

Node script when run in limited environment (e.g. php exe, cron, etc.) can throw cannot find module error. The error may look like this:

throw err;    ^Error: Cannot find module 'react'    at Function.Module._resolveFilename (module.js:326:15)    at Function.Module._load (module.js:277:25)    at Module.require (module.js:354:17)    at require (internal/module.js:12:17)    at Object. (code1:1:75)    at Module._compile (module.js:398:26)    at Object.Module._extensions..js (module.js:405:10)    at Module.load (module.js:344:32)    at Function.Module._load (module.js:301:12)    at Function.Module.runMain (module.js:430:10)

To fix this set NODE_PATH environment variable before command.

$ NODE_PATH=/usr/lib/nodejs:/usr/lib/node_modules /usr/bin/node code_file.js

Share this article: share on Google+ share on facebook share on linkedin tweet this submit to reddit

Comments

Click here to write/view comments