Skip to content

Using with ES6 modules #133

Closed
Closed
@jsonmaur

Description

@jsonmaur

In the case of using ES6 modules via import, configuring dotenv in the base file doesn't set the environment vars in sub-modules. For example:

index.js
import dotenv from 'dotenv'
dotenv.config({ silent: true })
import hello from './hello'
hello.js
console.log(process.env) /* this doesn't have the environment vars in it */

This is because babel compiles the above as

var dotenv = require('dotenv')
var hello = require('./hello')
dotenv.config({ silent: true })

It works if I do import 'dotenv/config' instead, but then I can't specify the silent flag, so it throws an error if .env doesn't exist (for example, if running in production). I think a simple solution would be to allow something such as import 'dotenv/register' for a one-line, silent setup of the variables.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions