5

I would like to ask if anyone know how to run forever that can load .env file.

Currently if we run forever start app.js, process.env.foo become undefined.

Tom Slabbaert
  • 21,288
  • 10
  • 30
  • 43
Najmi
  • 207
  • 1
  • 3
  • 9

8 Answers8

3

TLDR, You need to add the --workingDir path to your cronjob line.

forever -c "node -r dotenv/config" --workingDir app-workdir-path start app.js

Many previous answers but none of them really solve this specific use case.

To run forever with dotenv you'll need to do two things.

First is we need to use dotenv's preload feature, meaning we need forever to pass a node parameter to the process. we can do it by using the -c COMMAND flag forever has.

The second thing is related to how the dotenv package works. here is snippet from the source code:

let dotenvPath = path.resolve(process.cwd(), '.env')

What does process.cwd() do?

The process.cwd() method is an inbuilt application programming interface of the process module which is used to get the current working directory of the node.js process.

Meaning dovenv package want's to load the .env file from the working directory. so to solve this issue we can use forever's --workingDir flag to specify the actual working directory of the process.

And the final command will look like this:

forever -c "node -r dotenv/config" --workingDir app-workdir-path start app.js

Where app-workdir-path is the absolute path to the project directory.

Tom Slabbaert
  • 21,288
  • 10
  • 30
  • 43
2

What worked for me was to specify the full path:

require('dotenv').config({ path: '/opt/api/.env' });
tollbooth
  • 424
  • 4
  • 12
0

You can use dotenv package for this purpose. On your app entry, do this

require('dotenv').config({ path: '.env' })
Tuan Anh Tran
  • 6,807
  • 6
  • 37
  • 54
0

If you have added .env file in root directory of your project then you can use like this require('dotenv').config()

Or if you created your file .env with different location then in your code use require('dotenv').config({path : '/your/path/.env'})

Gerhard
  • 6,850
  • 8
  • 51
  • 81
user2124656
  • 7
  • 1
  • 7
0

I found your question and had the same issue. I don't think dotenv works with forever - At least not that I was able to get working. However, I think there's a workaround that you could employ. I was able to specify environment variables on the command line preceding the forever command, and forever passed those environment variables to my node app.

~$ ENV=production forever start yourApp.js

For more information about specifying environment variables on the command line, checkout this Stack Overflow question.

technogeek1995
  • 3,185
  • 2
  • 31
  • 52
0

I've had this issue with multiserver forever config.

You should include --workingDir parameter pointing to the root of your project directory in case you've included .env file in your root and using dotenv

Example:

  • Flexible config with minimum "hard coded" values
  • .env placed in root directory
  • "dotenv" used in form of dotenv.config()

Code for multiserver config in case of one server:

const fs = require('fs');
const path = require('path');

let foreverConfig = [
  {
    uid: 'scheduledJobsServer',
    append: true,
    watch: true,
    script: 'scheduledJobsServer.js',
    sourceDir: path.join(__dirname, '/server'),
    workingDir: path.join(__dirname)
  },
  {
    uid: 'mainServer',
    append: true,
    watch: true,
    script: 'server.js',
    sourceDir: path.join(__dirname, '/server'),
    workingDir: path.join(__dirname)
  }
];

try {
  fs.writeFileSync(
    path.join(__dirname, '/foreverConfig.json'),
    JSON.stringify(foreverConfig),
    { encoding: 'utf8' }
  );
  let consoleMessage = 'Init script success';
  console.log('\x1b[42m%s\x1b[0m', consoleMessage);
} catch (e) {
  console.log('Init script error:', e);
  process.exit(1);
}

Then run forever start foreverConfig.json

stasinua
  • 59
  • 6
0

Sometimes you have to call the node script from another directory. For instance, when running cron jobs. Here is what you can do:

cd /path/to/script/ && /usr/bin/forever start /usr/bin/node script.js

Now the .env file will load.

Thomas
  • 11
  • 2
0

The easiest command for me is

dotenv -e .env forever start build/index.js
unloco
  • 6,928
  • 2
  • 47
  • 58