Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Environment not loaded when module is imported #206

Closed
nareshbhatia opened this issue Jun 20, 2017 · 5 comments
Closed

Environment not loaded when module is imported #206

nareshbhatia opened this issue Jun 20, 2017 · 5 comments

Comments

@nareshbhatia
Copy link

nareshbhatia commented Jun 20, 2017

According to the docs:

As early as possible in your application, require and configure dotenv

I am doing that as the very first step in my application:

require('dotenv').config();
import OrderService from './OrderService';

However, in the OrderService module when I try to use an environment variable it is undefined:

import authService from 'authService';
const authInstance = authService(process.env.API_KEY);

export default class OrderService {
    ...
}

I suspect it has something to do with module loading order. Are there any known issues with this? Could it have something to do with require vs. ES6 import. While I am using require to load dotenv, rest of my application is written in ES6 and uses imports. I tried to load dotenv using an import, but that doesn't help.

@maxbeatty
Copy link
Contributor

imports are hoisted so order often isn't guaranteed. you might try requiring dotenv via node's CLI (example). I'll spend some time today updating the docs

@nareshbhatia
Copy link
Author

Thanks for the clarification, @maxbeatty.

maxbeatty added a commit that referenced this issue Jul 8, 2017
* document trimming behavior of values during parsing closes #197

* make comments conform to jsdoc 3 fixes #201

* document options for working with imports ref #206 #133 #89

* add exampe of using returned object from config
@gregorip02
Copy link

gregorip02 commented Aug 3, 2021

Solution Import the module as follows to solve this.

import 'dotenv/config';

// Here we can use .env variables in process.env

@dcolley
Copy link

dcolley commented Apr 28, 2023

Create a dotenv.js

import * as dotenv from 'dotenv'
dotenv.config()

In your index.js, import dotenv.js

// index.js
import './dotenv.js' // <== this import will resolve before the next import is processed
import OrderService from './OrderService';

@motdotla
Copy link
Owner

motdotla commented Jan 31, 2024

for others that run into this, we recommend using dotenvx.

  • works everywhere
  • supports multi-environment
  • adds optional encrypted envs

additionally, you no longer will need to wrestle with these import statements and edge cases across different versions of node and js/typescript/etc.

i'm personally using it for all my projects now. under the hood it still uses dotenv so no worries about a different parsing engine.

https://github.com/dotenvx/dotenvx

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants