1

I have followed every example I can find online and cannot get my pm2 config to correctly load my Node server. I keep getting the error: Error: Cannot find module 'dotenv/config' . I have my .env in a seperate location from my node server. I have created the ecosystem.config.js file by following their docs. Here is the file contents, full directory paths redacted:

module.exports = {
  apps : [{
    name        : "node-server-prod",
    script      : "/my/absolute/path/server.js",
    cwd         : "/my/absolute/path",
    node_args   : "-r dotenv/config",
    args        : "dotenv_config_path=relative/to/cwd/.env"
  }]
}

I tried both absolute and relative paths in the args section. Same with the script path.

I know I have dotenv installed in my node server path. It is not installed in the directory where my ecosystem.config.js file are and where I am calling: pm2 start ecosystem.config.js. I am almost certain it would not need to be installed in that directory. Any help would be much appreciated.

**Update: ** Adding full stack trace if that helps at all. enter image description here

2
  • Did you try installing with this npm install dotenv --save-dev ? Apr 16 at 16:14
  • I have verified that is installed in my node server folder. Not where I am running pm2 from though.
    – dmikester1
    Apr 16 at 17:11

1 Answer 1

0

I have tried your config file with my path and it is working and even i had move the env file outside the project directory but still it is loading.

module.exports = {
    apps: [{
    name: "node-server-prod",
    script: "./temp/server.js",
    cwd: "./",
    node_args: "-r dotenv/config",
    // i have tried with both env file inside and outside cwd
    args: "dotenv_config_path=../.env"
  }]
}

And also you can use require('dotenv').config(); to directly load variables in server file.

************ Edited ***********

Okay , I have put the pm2 file outside the project directory . and i got your error . And it is resolved , all you have to do is just update your path in following manner.

script: "./temp/server.js",
cwd: "./project_dir",
node_args: "-r dotenv/config",
// as your cwd is in project directory you can give path of env file considering that.
args: "dotenv_config_path=./.env"
4
  • Where is the ecosystem config file? Is that the same folder you are running the start command from?
    – dmikester1
    Apr 17 at 16:44
  • In the root directory , env and ecosystem config file are at same location
    – Jack
    Apr 18 at 6:53
  • And do you have dotenv installed in the project_dir location? It seems there is no way to install that package globally.
    – dmikester1
    2 days ago
  • yes , its installed in project dir only. No need to install globally. All you have to do is give appropriate path.
    – Jack
    3 hours ago

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.