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.
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.
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.
What worked for me was to specify the full path:
require('dotenv').config({ path: '/opt/api/.env' });
You can use dotenv package for this purpose. On your app entry, do this
require('dotenv').config({ path: '.env' })
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'})
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.
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:
.env
placed in root directorydotenv.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
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.