question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Importing dotenv in ES6

See original GitHub issue

Support on ES6 has been tricky for me. Yesterday I tried preloading dotenv with iojs -r dotenv/config index.js. However, it didn’t work and I couldn’t understand why. My app did nothing. After running the app, a second or two later it would finish without doing anything.

I ended up with a very simple way to use dotenv. I simply import it like this:

import {} from 'dotenv/config'
import somethingElse from 'somethingElse'
...
[the rest of your code]

This works because of how ES6 modules imports modules. Before doing anything else in a file (say index.js) it does all the imports first. It does a depth first traversal of these imports, executing any code inside it. If we import dotenv first, it will execute config and add any env variables before doing anything else in the code.

From ES6 In Depth: Modules:

When you run a module containing an import declaration, the modules it imports are loaded first, then each module body is executed in a depth-first traversal of the dependency graph, avoiding cycles by skipping anything already executed.

So, importing dotenv on the first line of a bootstrap file in an app will set the env vars for anything that might use them.

I suggest we add a brief section about importing dotenv with ES6 modules to the README. But before sending a pull request and wanted to open it up for discussion here.

Any thoughts?

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:146
  • Comments:47 (5 by maintainers)

github_iconTop GitHub Comments

327reactions
jcblwcommented, Sep 10, 2015

I believe since we are not exporting out a key fordotenv the default import behavior of babel @maxbeatty mentions should work.

import dotenv from 'dotenv'
dotenv.config()

I would also be interested in how your running into an issue with preloading the module. Can you post the version of node/iojs and dotenv that you are using?

183reactions
motdotlacommented, Sep 10, 2015

@jcblw you’ve done some work with ES6 right?

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to use express js with dotenv and ES6 modules
import dotenv from "dotenv" import express from "express" dotenv.config() const app = express() const port = process.env.
Read more >
ES6 import happening before .env import - Stack Overflow
In your original code you dont import dotenv, you explicitly require it, which will always happen after the imports if they are in...
Read more >
dotenv - npm
Start using dotenv in your project by running `npm i dotenv`. There are 31890 other projects in the npm registry using dotenv.
Read more >
dotenv with es6 Code Example - Code Grepper
Answers related to “dotenv with es6” ... how to import dotenv in node js and env · using dotenv in js · dotenv...
Read more >
dotenv - npm.io
require('dotenv').config() console.log(process.env) // remove this after you've confirmed it is working .. or using ES6? import * as dotenv from 'dotenv' ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found