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

Add CLI Flag to specify .env file location #3435

Closed
damianobarbati opened this issue Aug 19, 2019 · 12 comments
Closed

Add CLI Flag to specify .env file location #3435

damianobarbati opened this issue Aug 19, 2019 · 12 comments

Comments

@damianobarbati
Copy link

damianobarbati commented Aug 19, 2019

Add a --env <env-file> flag to the cli to have the ability to store env files outside of root directory and/or have custom env filenames

Original issue:

  1. Parcel is currently reading from '.env' file in the folder, but what is the .env file as another name or is in subfolder? Is it possible to give a custom path for the env file?

  2. I use this very simple package of mine to inject env vars into node processes and I was expecting to find env vars available to the parcel process injectable in my js app but it's not so.
    I.E:

ENVK=./config/development.env npx -r 'envk' parcel public/index.html --open --no-cache

The development.env variables are available to parcel but no in my js app: is this intended? (NB: webpack behaves now similarly, envs must be explicitly passed when building the js bundle)

@DeMoorJasper
Copy link
Member

DeMoorJasper commented Aug 19, 2019

Taking environments directly from the machine you're building your code with is a security risk and might cause conflicts between the Parcel runtime and environment as we also use environment variables for Parcel itself.

I'm pretty sure it's still supported in the cli though, not sure why it isn't working for you

@damianobarbati
Copy link
Author

@DeMoorJasper do you have any idea where it is in the docs? I really didn't find a way to configure this

@DeMoorJasper
Copy link
Member

There's an entire section on environment variables: https://en.parceljs.org/env.html

@damianobarbati
Copy link
Author

Yes but there's no way to provide a specific path for the env file if none of the options can be applied to the project (i.e: --env config/development.env)

@DeMoorJasper
Copy link
Member

I use this very simple package of mine to inject env vars into node processes and I was expecting to find env vars available to the parcel process injectable in my js app but it's not so.

Ow no there isn't I meant injecting env variables should work in theory, or make a symlink from dev.env to config/dev.env that should also work...

@damianobarbati
Copy link
Author

You're right, that's a way to work it around but I didn't want to pollute the root dir with all the .env files (or associated symlinks 🙃).

Anyway if the ability to customize the .env location in CLI is not considered for the near future we can close this issue (sigh 😢).

Your call!

@DeMoorJasper
Copy link
Member

@damianobarbati it might be an option for v2, I'll leave it open.
This is a pretty advanced use-case though and v2 allows injecting env variables through the api, so that would also be a workaround.

@DeMoorJasper DeMoorJasper changed the title Custom env filename Add CLI Flag to specify .env file location Aug 21, 2019
@damianobarbati
Copy link
Author

damianobarbati commented Nov 22, 2019

@DeMoorJasper I adapted to the standard, so .env.< environment >

@Cristy94
Copy link

So, how can you specifiy the .env file that parcel v2 should use?

I want to build different versions of the product that have different .env files (eg. .env.prod.premium), how can I pass that .env file to parcel build?

@mischnic
Copy link
Member

via NODE_ENV:

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

@lxchurbakov
Copy link

Hey guys! Anyone knows how to specify the file directly? Looks like parcel searches in process.cwd which is not how dotenv works and thus parcel won't work properly with namespaces - e.g. cd project && npm run dev -w client-side will look for .env in /project instead of /project/client-side.

@lxchurbakov
Copy link

Moreover, doc specifies that:

.env files are to be stored alongside the package.json that contains your parcel-bundler dependency.

However, in fact parcel looks for the .env files in a folder where node_modules is stored, which might not be the same directory.

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

5 participants