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

importing dotenv using ES6 imports #114

Closed
tikotzky opened this issue Dec 20, 2015 · 4 comments
Closed

importing dotenv using ES6 imports #114

tikotzky opened this issue Dec 20, 2015 · 4 comments

Comments

@tikotzky
Copy link

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...

import dotenv from 'dotenv';
dotenv.load();

import foo from 'foo'; // this file reads from process.env

babel will transpile it to something equivalent to

var dotenv = require('dotenv');
var foo = require('foo');
dotenv.load();

(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 before dotenv.load() is called, so it won't process.env populated.

A solution (similiar to what was suggested in #89) is to change the code to look like

import 'dotenv/config';

import foo from 'foo'; // this file reads from process.env

What that does is import this file which essentially calls dotenv.config() as a side effect. This way by the time the 'foo' is imported dotenv 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 a load.js file in the root of the project that would just contain

require('./lib/main').load()

You would then be able to do something like this in es6

import 'dotenv/load';

import foo from 'foo'; // this file reads from process.env

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! 😄

@maxbeatty
Copy link
Contributor

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)

@tikotzky
Copy link
Author

I'm not sure if i'm fully convinced to use preloading over import 'dotenv/config'; 😄
In any case I just wanted to document this here in case anyone was bumping into the same issue I was.

And once I have an open issue I'd also like to thank you guys for putting out an awesome project 👍

@OKNoah
Copy link

OKNoah commented Dec 22, 2015

Could clear instructions on this go into the readme?

@dandv
Copy link

dandv commented May 8, 2020

This is an issue with babel transpiling and not with ES6. For newer projects, preloading

I'm running into this issue with ts-node but the link to preloading is broken. Is there a modern solution for importing that loads variables with ts-node?

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

4 participants