Parcel: .env files are ignored

Created on 11 Feb 2020  ยท  10Comments  ยท  Source: parcel-bundler/parcel

๐Ÿ› 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

Bug โœจ Parcel 2

All 10 comments

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 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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

philipodev picture philipodev  ยท  3Comments

davidnagli picture davidnagli  ยท  3Comments

davidnagli picture davidnagli  ยท  3Comments

466023746 picture 466023746  ยท  3Comments

dsky1990 picture dsky1990  ยท  3Comments