Search code examples
node.jspm2

Global module MODULE_NOT_FOUND if started from pm2


I have the emailjs package installed globally. My NODE_PATH is correctly showing the value from npm -g root: /usr/lib/node_modules. In Node REPL, the following runs from any folder without error:

var email = require("emailjs/email");

However, in a Node script started via pm2, var email = require("emailjs/email") is resulting in MODULE_NOT_FOUND:

Error: Cannot find module 'emailjs/email'

What could be happening?


Solution

  • Somehow, the environment for pm2 is frozen at what it was when it was first run. My very first app didn't have the NODE_PATH parameter set. It was set only later. So, even though the environment has the NODE_PATH parameter set, pm2 doesn't see it.

    Therefore, there is a need to start the new app with the --update-env option.