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.
.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',
}
config.ts should export
{
project_name: 'My Project',
api_url: 'http://localhost:8074'
}
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.
See above
| Software | Version(s) |
| ---------------- | ---------- |
| Parcel | 2.0.0-alpha.3
| Node | 13.8
| npm/Yarn | yarn 1.22
| Operating System | Arch Linux
i used to config my env configure in npm hooks like:
json
// 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 theNODE_ENVenvironment 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
.envfiles to source control with the exception of.env*.localfiles. The local.envfiles 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.