Open ranisalt opened 4 years ago
i used to config my env configure in npm hooks like:
// package.json
{
...
"scripts": {
"start": "APP_ENV=local parcel serve",
"build:dev": "APP_ENV=develop parcel build",
"build:prod": "APP_ENV=production parcel build"
},
...
}
wish some helps for u~
@cdll I have several more env vars so that would be very annoying to do.
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.
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?
Or should the root .env.local be more important than the package .env.local?
I'd say no.
Hi! I'm trying to deploy a project bundled with Parcel using Netlify. I am following Robert Cooper's instructions:
Specifying Environments When running the normal parcel serve command (e.g. parcel index.html), the environment (specified by the
NODE_ENV
environment variable) will be development by default. To specify an environment other than development while using the parcel serve command, you can specify the environment when running the parcel serve command:
NODE_ENV=staging parcel index.html
When running the parcel build command (e.g. parcel build index.html), the environment will be production by default. Again, you can specify a specific environment when running the build command:
NODE_ENV=staging parcel build index.html
And:
You should commit all your
.env
files to source control with the exception of.env*.local
files. The local.env
files should only be used to tweak your app's configurations when running the app locally.
I am currently not commiting the .env
file but I'm using the environment specification mentioned above. It's not providing access to staging variables during build. Is this due to the bug in discussion?
what's the current status of this issue? Seems very important to still open.
Same here. In my case I'm trying to use NODE_ENV
to do a simple conditional import:
async function renderApp() {
if (process.env.NODE_ENV === 'development') {
// eslint-disable-next-line no-console
await import('stuff');
}
render(<App />, document.getElementById('root'));
}
I'm using cross-env
to set NODE_ENV
, as so: cross-env NODE_ENV=development parcel ./src/index.html
. But it doesn't work; moreover, if I console log process.env.NODE_ENV
it prints out production
:question:. Also tried putting a .env.development
but it doesn't get picked up by parcel.
Using latest version (2.0.0-beta1).
@alessandrojcm You don't need to specify NODE_ENV explicitly in your case. parcel build
automatically sets production
and in watch mode, development
. Please provide a full example.
Stumbled upon the same problem today. Would be good to provide some options to overwrite projectRoot
to be related to the given package parcel
is used for in monorepo setup.
projectRoot
that points to the root is causing also problems at least with babel (see #7075)
In my case, i solved it by adding the entrie file path in package.json.
"scripts": {
"start": "parcel",
"build": "parcel build src/index.html"
}
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.
Hey, is this still going to happen? Just came across this issue last night and I was really scratching my head figuring this out. Reading .env files from sub packages sounds more reasonable to me. Thanks! @ranisalt
In my case, i solved it by adding the entrie file path in package.json.
"scripts": { "start": "parcel", "build": "parcel build src/index.html" }
I tried this, but it didn't work in my case.
In my case, i solved it by adding the entrie file path in package.json.
"scripts": { "start": "parcel", "build": "parcel build src/index.html" }
This works for me too thanks!!!
I just ran into this problem as well. I will have to completely stop using parcel for any projects if this isn't updated.
In my case, i solved it by adding the entrie file path in package.json.
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
@mischnic, @devongovett can you confirm that
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.
is fixed?
It still does not work for me, testing with build ^2.0.0-nightly.1071 by not working I mean that only .env files in the root of the project are loaded, .envs in monorepo packages are ignored.
I'm also still having trouble loading .env.local
from my monorepo package. Loading the env variables only works when the .env.local
file is placed in the root of the project.
I'm using the latest 2.6.0
I am using parcel@2.6.0
and I am having the same issue. Monorepo with multiple .env
in each package
Yes, currently it only searches for dotfiles in the project (=monorepo) root
Spent a couple days trying to solve this for myself. I tried using dot-env
and env-cmd
directly to reference the correct env files instead of letting parcel look for them in the project root - no dice for me at least.
The only feasible "workaround" I stumbled upon was the one mentioned in here https://github.com/parcel-bundler/parcel/issues/4264, which is to use cross-env
. Of course, you're not using .env
files at that point, but it can get the job done depending on how many variables you need to set.
A pretty easy way to use the cross-env
workaround and still use your .env files is to do
cross-env $(cat .env.development) parcel ...
Are they planning to fix this bug?
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).
For anyone unsatisfied with the solutions offered so far, an easy fix for me is using dotenv-cli and running Parcel like this:
dotenv -- parcel src/index.html --port 3001
It loads the environment variables from the nearest .env
file instead of only from the monorepo root.
š 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
.env.development
config.ts
š¤ Expected Behavior
config.ts
should exportšÆ Current Behavior
config.ts
exportsUpon further inspection (inserted
debugger
inside config.ts) I see thatprocess.env.NODE_ENV
is set todevelopment
(as expected), butprocess.env.API_ENDPOINT
andprocess.env.PROJECT_PACKAGE
are bothundefined
.š» Code Sample
See above
š Your Environment