parcel: .env files are ignored in monorepo subpackages
🐛 bug report
I’m trying to provide environment variables to select a couple of values on build time, however the env files (as described here) are being ignored.
🎛 Configuration (.babelrc, package.json, cli command)
.env
PROJECT_PACKAGE=myproject
.env.development
API_ENDPOINT=localhost
config.ts
export default {
project_name:
process.env.PROJECT_PACKAGE === 'myproject'
? 'My Project'
: 'Not My Project',
api_url:
process.env.API_ENDPOINT === 'localhost'
? 'http://localhost:8074'
: 'https://api.example.com',
}
🤔 Expected Behavior
config.ts
should export
{
project_name: 'My Project',
api_url: 'http://localhost:8074'
}
😯 Current Behavior
config.ts
exports
{
project_name: 'Not My Project',
api_url: 'https://api.example.com'
}
Upon further inspection (inserted debugger
inside config.ts) I see that process.env.NODE_ENV
is set to development
(as expected), but process.env.API_ENDPOINT
and process.env.PROJECT_PACKAGE
are both undefined
.
💻 Code Sample
See above
🌍 Your Environment
Software | Version(s) |
---|---|
Parcel | 2.0.0-alpha.3 |
Node | 13.8 |
npm/Yarn | yarn 1.22 |
Operating System | Arch Linux |
About this issue
- Original URL
- State: open
- Created 4 years ago
- Comments: 25 (9 by maintainers)
Commits related to this issue
- Добавлена ссылка на parcel-bundler/parcel/issues/4110 — committed to khusamov/box2d by khusamov 2 years ago
The problem is parcel reads the env vars from the root of the package and not the packages inside this monorepo. I’ll try to make a PR and fix this.
A pretty easy way to use the
cross-env
workaround and still use your .env files is to doYes, currently it only searches for dotfiles in the project (=monorepo) root
https://github.com/parcel-bundler/parcel/blob/3b3b13557dba9d1a403c86550915c6013dd78dfe/packages/core/core/src/loadDotEnv.js#L18-L35
Yeah that’s probably the same issue to those comments. It would be good for parcel to read all env files from the monorepo root down to the package dir, updating values as it travels down.
Or should the root .env.local be more important than the package .env.local?
I just want to point out that the root cause of this issue is being discussed here: https://github.com/parcel-bundler/parcel/issues/7579 and that this workaround while imperfect is doing the job (mostly).
This should be fixed now in the latest nightly version by https://github.com/parcel-bundler/parcel/pull/7537
Try using
yarn add parcel@nightly
This works for me too thanks!!!
In my case, i solved it by adding the entrie file path in package.json.
what’s the current status of this issue? Seems very important to still open.
@cdll I have several more env vars so that would be very annoying to do.
i used to config my env configure in npm hooks like:
wish some helps for u~