Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

.env files are ignored in monorepo subpackages #4110

Open
ranisalt opened this issue Feb 11, 2020 · 25 comments
Open

.env files are ignored in monorepo subpackages #4110

ranisalt opened this issue Feb 11, 2020 · 25 comments

Comments

@ranisalt
Copy link
Contributor

🐛 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
@cdll
Copy link

cdll commented Feb 12, 2020

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~

@ranisalt
Copy link
Contributor Author

@cdll I have several more env vars so that would be very annoying to do.

@ranisalt
Copy link
Contributor Author

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.

@mischnic
Copy link
Member

Related: #3942 (comment), #3942 (comment)

@ranisalt
Copy link
Contributor Author

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?

@mischnic
Copy link
Member

Or should the root .env.local be more important than the package .env.local?

I'd say no.

@ghost
Copy link

ghost commented Jun 12, 2020

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?

@mtxr
Copy link
Contributor

mtxr commented Jul 3, 2020

what's the current status of this issue? Seems very important to still open.

@alessandrojcm
Copy link

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 ❓. Also tried putting a .env.development but it doesn't get picked up by parcel.

Using latest version (2.0.0-beta1).

@mischnic
Copy link
Member

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

@desfero
Copy link

desfero commented Nov 9, 2021

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)

@rafaelnai
Copy link

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

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

@charIeszhao
Copy link

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

@hnordt
Copy link

hnordt commented Mar 4, 2022

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.

@beefs
Copy link

beefs commented Mar 4, 2022

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

@rubixibuc
Copy link

I just ran into this problem as well. I will have to completely stop using parcel for any projects if this isn't updated.

@mischnic
Copy link
Member

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 #7537

Try using yarn add parcel@nightly

@d-lach-aliz
Copy link

d-lach-aliz commented Apr 28, 2022

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

@ghannamr
Copy link

ghannamr commented May 25, 2022

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

@tomtobac
Copy link

I am using parcel@2.6.0 and I am having the same issue. Monorepo with multiple .env in each package

@mischnic
Copy link
Member

mischnic commented May 31, 2022

Yes, currently it only searches for dotfiles in the project (=monorepo) root

const dotenvFiles = [
'.env',
// Don't include `.env.local` for `test` environment
// since normally you expect tests to produce the same
// results for everyone
NODE_ENV === 'test' ? null : '.env.local',
`.env.${NODE_ENV}`,
`.env.${NODE_ENV}.local`,
].filter(Boolean);
let envs = await Promise.all(
dotenvFiles.map(async dotenvFile => {
const envPath = await resolveConfig(
fs,
filePath,
[dotenvFile],
projectRoot,
);

@mischnic mischnic reopened this May 31, 2022
@mischnic mischnic changed the title .env files are ignored .env files are ignored in monorepo subpackages May 31, 2022
@havenbarnes
Copy link

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

@havenbarnes
Copy link

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

@khusamov
Copy link

Are they planning to fix this bug?

@jraoult
Copy link

jraoult commented Nov 12, 2022

I just want to point out that the root cause of this issue is being discussed here: #7579 and that this workaround while imperfect is doing the job (mostly).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests