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

parcel dotenv doesn't work properly #2299

Closed
dancespiele opened this issue Nov 19, 2018 · 8 comments
Closed

parcel dotenv doesn't work properly #2299

dancespiele opened this issue Nov 19, 2018 · 8 comments

Comments

@dancespiele
Copy link

dancespiele commented Nov 19, 2018

🐛 bug report

process.env doesn't content the properties set in .env files

🎛 Configuration (.babelrc, package.json, cli command)

.env:

API_KEY=0123456

package.json:

"scripts": {
    "build": "parcel build index.html",
    "watch": "parcel -p 3000 index.html",
    "test": "jest"
  },

🤔 Expected Behavior

process.env should have the property API_KEY in this case

console.log(process.env)   //  Object {API_KEY: 0123456}

😯 Current Behavior

console.log(process.env)   //  Object {}

💁 Possible Solution

🔦 Context

💻 Code Sample

🌍 Your Environment

Software Version(s)
Parcel 1.10.3
Node v11.1.0
yarn 1.12.3
Operating System manjaro (Archlinux)
@devongovett
Copy link
Member

Accessing the entire process.env object is not supported for security reasons since it would mean exposing all of the environment variables on your system. If you access a specific variable like process.env.API_KEY it should work.

@dancespiele
Copy link
Author

That's right, finally my problem was different :). Great job with parcel ;)

@Yaojian
Copy link

Yaojian commented Apr 3, 2019

If you access a specific variable like process.env.API_KEY it should work.

Please add this to doc ~~~~ https://parceljs.org/env.html

@niksseif
Copy link

niksseif commented Jul 26, 2019

@devongovett I am using parcel with react and I am trying to access my .env file API key and when I console.log the process.env it gives an empty object but once I add process.env.REACT_APP_API_KEY it returns undefined !!! Can someone tell me what is going on?
Here is my package.json:
"scripts": {
"clear-build-cache": "rm -rf .cache/ dist/",
"dev": "parcel src/index.html",
"build": "parcel build src/index.html",
"dev:mock": "cross-env OPEN-INV=mock parcel src/index.html",
"format": "prettier --write "src//*.{js,jsx}"",
"lint": "eslint "src/
/*.{js,jsx}" --quiet",
"test": "echo "Error: no test specified" && exit 1"
},

@mischnic
Copy link
Member

@niksseif
console.log(process.env) won't work because including all of your env variables has security issues.

This should work:
.env (in the same folder as package.json):

REACT_APP_API_KEY="1238uasd"

code:

console.log(process.env.REACT_APP_API_KEY);

@Morglod
Copy link

Morglod commented Dec 26, 2019

@mischnic what security reasons? There is no cases
It's just super uncomfortable for 'simple bundler'

@mathieucaroff
Copy link

It'd be great to have a way to specify the set of variables which needs to be present in the bundle. I think a sensible thing to do would be to always include all variables present in the .env* files, but I'm not it's the case currently (with parcel 1.12.4).

I'd like to check out the code if someone can point me the file handling this.

@mischnic
Copy link
Member

mischnic commented Jan 7, 2020

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

7 participants