Dotenv: How to integrate with Meteor?

Created on 17 Sep 2017  路  4Comments  路  Source: motdotla/dotenv

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?

Most helpful comment

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

All 4 comments

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`
});
Was this page helpful?
0 / 5 - 0 ratings

Related issues

AndersDJohnson picture AndersDJohnson  路  4Comments

Jakobud picture Jakobud  路  3Comments

ffxsam picture ffxsam  路  4Comments

shellscape picture shellscape  路  3Comments

tikotzky picture tikotzky  路  4Comments