-
-
Notifications
You must be signed in to change notification settings - Fork 873
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
Comments
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 |
Thanks for the clarification, @maxbeatty. |
Solution Import the module as follows to solve this. import 'dotenv/config';
// Here we can use .env variables in process.env |
Create a import * as dotenv from 'dotenv'
dotenv.config() In your // index.js
import './dotenv.js' // <== this import will resolve before the next import is processed
import OrderService from './OrderService'; |
for others that run into this, we recommend using
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
|
According to the docs:
I am doing that as the very first step in my application:
However, in the OrderService module when I try to use an environment variable it is undefined:
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. ES6import
. While I am usingrequire
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.The text was updated successfully, but these errors were encountered: