parcel: Destructuring doesn't work with process.env
๐ bug report
Destructuring doesnโt work with process.env
.
๐ Configuration (.babelrc, package.json, cli command)
{
"presets": ["@babel/preset-env"]
}
๐ค Expected Behavior
const {PWD, NODE_ENV} = process.env;
console.log(PWD, NODE_ENV);
The console should print the variables, something like /home/user/parcel development
.
๐ฏ Current Behavior
The console prints undefined undefined
.
๐ Possible Solution
๐ฆ Context
I am trying to follow ESLintโs no-process-env rule. I want to have only one place (one line) in my codebase where I read variables from process.env
, so I have a config.js
file with the following code:
//eslint-disable-next-line no-process-env
const {API_URL, NODE_ENV} = process.env;
export {
API_URL,
NODE_ENV
};
๐ป Code Sample
const {PWD, NODE_ENV} = process.env;
console.log(PWD, NODE_ENV);
๐ Your Environment
Software | Version(s) |
---|---|
Parcel | v1.10.3 |
Node | v11.0.0 |
npm/Yarn | npm 6.4.1 |
Operating System | Ubuntu 18.04 |
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 7
- Comments: 16 (3 by maintainers)
Currently, this
is processed by babel into:
But Parcelโs env visitor (https://github.com/parcel-bundler/parcel/blob/master/packages/core/parcel-bundler/src/visitors/env.js) only looks out for
process.env
and looks up the key that follows: https://github.com/parcel-bundler/parcel/blob/6566e160affa161daa6d5486139e6059f685ba3a/packages/core/parcel-bundler/src/visitors/env.js#L6Lookup hack:
Remaining issue: removing the
_process$env
variable and also determining the name of_process$env
at runtime (it could be different in some cases).@mytee306 It does indeed, but only if you extract exactly one property. Strange.
@xiaoxiangmoe โDonโt use itโ is not a solution. The bug should be fixed.
Not sure how relevant/useful what Iโm about to say is, but this happens when using Babel by itself too.
edit: as it turns out, the issue in my case may have been related to using an older NodeJS version (I was running multiple projects simultaneously, one of them a legacy app that required node <=8.)
Just to discard the issue in your case is not the same as mine, Iโd check having the latest updates.
Having the same problem here. Had get them one by oneโฆ
Cross referencing the same bug report for webpack: https://github.com/webpack/webpack/issues/5392.