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

Most upvoted comments

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 do

cross-env $(cat .env.development) parcel ...

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

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

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

In my case, i solved it by adding the entrie file path in package.json.

"scripts": {
    "start": "parcel",
    "build": "parcel build src/index.html"
 }

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:

// 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~