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

Using with ES6 modules #133

Closed
jsonmaur opened this issue Apr 3, 2016 · 26 comments
Closed

Using with ES6 modules #133

jsonmaur opened this issue Apr 3, 2016 · 26 comments

Comments

@jsonmaur
Copy link

jsonmaur commented Apr 3, 2016

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.

@jsonmaur
Copy link
Author

jsonmaur commented Apr 3, 2016

I see that there were a few other issues for this in the past, but I didn't see that a solution was ever reached? I know preloading the module was recommended, but unfortunately that's not an option in this case. I'd be happy to submit a PR for this if you'd like.

@jcblw
Copy link
Collaborator

jcblw commented Apr 7, 2016

hey @jsonmaur you can set options using the preload script. You can pass in the options via the a arg on the command.

node -r dotenv/config your_script.js dotenv_config_silent=true

@jcblw
Copy link
Collaborator

jcblw commented Apr 15, 2016

closing this, if this does not work for you let me know

@jcblw jcblw closed this as completed Apr 15, 2016
@ngzhian
Copy link

ngzhian commented Oct 21, 2016

I faced this problem as well, and I couldn't figure out how to use node preload script because I was running nodemon (this is for development). What I did was to move the dotenv call into a separate file and then import it in index.js.

index.js
import _ from './env'
import hello from './hello'
env.js
import dotenv from 'dotenv'
dotenv.config({ silent: true })

Hope this is helpful for anyone who faces this issue.

@StevePavlin
Copy link

Thanks @ngzhian this fix worked for me!

@Elyx0
Copy link

Elyx0 commented May 30, 2017

import './env';

Should do it if you don't want to pollute the scope

maxbeatty added a commit that referenced this issue Jul 8, 2017
* document trimming behavior of values during parsing closes #197

* make comments conform to jsdoc 3 fixes #201

* document options for working with imports ref #206 #133 #89

* add exampe of using returned object from config
@morajabi
Copy link

I doesn't work for me with babel-node.

@sa-adebayo
Copy link

@morajabi Have you found a workaround for babel-node?

@morajabi
Copy link

@sa-adebayo Yes and the solution is simply don't use babel-node. It's advised to run your code with babel and dotenv this way:

node --require 'dotenv/config' --require 'babel-register'

now you don't need to require or import dotenv nor babel-register inside your code.

@LalitKushwah
Copy link

How to use dotenv with stenciljs ?

@emdagon
Copy link

emdagon commented Jul 20, 2018

This worked for me:

babel-node src/index.js --require 'dotenv/config'

Keep in mind that it won't work if you put the --require argument before the script (babel-node --require 'dotenv/config' src/index.js)

@dhurlburtusa
Copy link

I was able to get babel-node to work with putting the --require option before the script if I provided a complete path, absolute or relative, instead of the module name. For example:

babel-node --require node_modules/dotenv/config script

The above call works as an NPM script with babel-node installed locally (it probably works from the command-line too if babel-node is installed globally but I didn't test it).

To run it from the command-line with babel-node installed locally, call like:

node_modules/.bin/babel-node --require node_modules/dotenv/config script

Although adding node_modules/ may not be pretty, the require option now won't look like it is being used by script. Also, '--require' and 'dotenv/config' won't show in process.argv. But any options listed after script will show up in process.argv as expected. Note: -- not required. If used, then it will show up in process.argv. For example:

babel-node --require node_modules/dotenv/config script these opts are in process.argv
// script.js
console.log(process.argv); // [ 'node', 'full/path/to/script', 'these', 'opts', 'are', 'in', 'process.argv' ]

Note: This was tested with @babel/node version 7.0.0-beta.51 with node version v8.11.2.

@normancarcamo
Copy link

I'm currently using this plugin of babel and it's working in the same way

babel-plugin-inline-dotenv

The difference is that I don't need to require and load it in my entry points.

/* .babelrc */
{
  "presets": ["env"],
  "plugins": ["inline-dotenv"]
}

Hope my 2 cents help.

@dmark
Copy link

dmark commented Dec 23, 2018

Trying to do this in a TypeScript environment:

env.ts:

import dotenv from 'dotenv'
dotenv.config({ silent: true })

After installing @types/dotenv, I now get a 'has no default export.' error on dotenv. I am pretty new to JS and completely new to TS. The following is working for me but I have no idea whether this is an appropriate way to handle the import or not:

env.ts:

import * as dotenv from 'dotenv'
dotenv.config()

@maxbeatty
Copy link
Contributor

@dmark see #362 for an example of using dotenv in a TypeScript environment. Your import * as dotenv is totally fine. Destructuring in the example (import { config } from "dotenv") is another approach that gets the same result.

@KresimirCosic
Copy link

I am new to Express with TypeScript, but nothing of this works for me. I am getting an error dotenv module does not exist at all.

@mytecor
Copy link

mytecor commented May 27, 2021

Compact:

(await import('dotenv')).config()

@zomars
Copy link

zomars commented Apr 28, 2022

Compact:

(await import('dotenv')).config()

There's many environments that doesn't support top-level awaits.

@bitdom8
Copy link

bitdom8 commented Jul 3, 2022

Please check here: https://stackoverflow.com/a/72847459/12002600

@nickopaijo
Copy link

import dotenv from 'dotenv'
export default dotenv.config({ silent: true })

@AbdulRehman-z
Copy link

Not Worked

@bitdom8
Copy link

bitdom8 commented Aug 21, 2022

What error you get after doing this @AbdulRehmanConqueror

@kirtan-desai
Copy link
Contributor

import dotenv from 'dotenv'
export default dotenv.config({ silent: true })

isn't silent deprecated?

@nickopaijo
Copy link

import dotenv from 'dotenv'
export default dotenv.config({ silent: true })

isn't silent deprecated?

as i know not deprecated, this code works for me.
image

@kirtan-desai
Copy link
Contributor

import dotenv from 'dotenv'
export default dotenv.config({ silent: true })

isn't silent deprecated?

as i know not deprecated, this code works for me.
image

silent option was removed in v3. I guess they don't have a warning when silent is used.

@nickopaijo
Copy link

import dotenv from 'dotenv'
export default dotenv.config({ silent: true })

isn't silent deprecated?

as i know not deprecated, this code works for me.
image

silent option was removed in v3. I guess they don't have a warning when silent is used.

whoops i'm sorry for my mistakes

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