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
importing dotenv using ES6 imports #114
Comments
This is an issue with babel transpiling and not with ES6. For newer projects, preloading is a better way to go since load order is guaranteed, you need less code, and have more freedom for deployment in different environments (not tied to flat file) |
I'm not sure if i'm fully convinced to use preloading over And once I have an open issue I'd also like to thank you guys for putting out an awesome project 👍 |
Could clear instructions on this go into the readme? |
I'm running into this issue with |
I figured out some trickiness that I had with es6 imports being transpiled with babel.
The issue is that if you have some code that looks like this...
babel will transpile it to something equivalent to
(note: the above is what the transpiled code would be functionally equivalent to.
If you want to see babels exact output you can look here)
As you can see, babel is moving up all the imports. This is because the es6 modules spec requires that all imports are loaded depth first before executing the module body.
The issue is that
foo.js
ends up getting required beforedotenv.load()
is called, so it won'tprocess.env
populated.A solution (similiar to what was suggested in #89) is to change the code to look like
What that does is import this file which essentially calls
dotenv.config()
as a side effect. This way by the time the'foo'
is importeddotenv
has already loaded.The only downside of this solution is that it will try to parse
argv
(which is not necessary). The way to work around that would be to add aload.js
file in the root of the project that would just containYou would then be able to do something like this in es6
Let me know if you're ok with adding a
load.js
and I'll open a PR to add the files and update the example.Thanks! 😄
The text was updated successfully, but these errors were encountered: