I'm passing some environment variables to Meteor application. The meteor-dotenv suggests that use this module instead, but I can't get it working. The error message is as below:
Error: ENOENT: no such file or directory, open '.env'
I put my .env in my root folder. Does anybody know how to use it with Meteor?
I would try specifying the path to your .env file (require('dotenv').config({ path: './example/.env' })) and debugging from there. Often when the process is started by another tool like Meteor, the assumed working directory is not what you think.
Same issue. Solution:
1) Create a private folder in the project root folder and include the .env file there
2) In the script where you load dotenv use Assets to get the full path to the file:
/* global Assets */
import dotenv from 'dotenv';
dotenv.config({
path: Assets.absoluteFilePath('.env'),
});
Note I didn't imported Assets as a es6 module since it is not (already?) supported by Meteor
More info about Assets and the private folder: assets docs
For anyone else interested, I wanted to avoid putting the .env file in the Assets, as I didn't want it bundled with the build process.
I kept the .env file in the project root and used the following at the top of the main server script.
import dotenv from 'dotenv'
dotenv.config( {
path: `${process.env.PWD}/.env`
} )
Based on @roark answer, here is how I got mine to work.
import dotenv from 'dotenv'
Meteor.startup(() => {
dotenv.config({
path: `${process.env.PWD}/.env`
});
Most helpful comment
Same issue. Solution:
1) Create a
privatefolder in the project root folder and include the .env file there2) In the script where you load dotenv use Assets to get the full path to the file:
Note I didn't imported Assets as a es6 module since it is not (already?) supported by Meteor
More info about Assets and the private folder: assets docs