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 basic .env support. #258

Merged
merged 7 commits into from
Jan 3, 2018
Merged

Conversation

gongpeione
Copy link
Contributor

this.envTable = {};
const envRegex = /^(\w+)\s*=\s*([\s\S]+)/;
const envFile =
process.env.NODE_ENV === 'development' ? ['.dev.env'] : ['.env'];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is kind of tricky. NODE_ENV is something that was popularized by Express. I know most of us would probably end up using NODE_ENV, but it's really not required to do so. If parcel is going to look for that exact prop, then can you also add it in the documentation somewhere?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes I agree. And I am not sure if it is a good idea seperate .env and .dev.env this way.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gongpeione as long as it is documented, I think it's okay. 😄

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How to handle env other than dev and prod? e.g. dev/uat/prod.

@devongovett
Copy link
Member

I'm on board with this idea (loading environment variables from a file). Can we use this module to actually load and parse the files though? https://github.com/motdotla/dotenv We can do that once on bundler startup, rather than for each asset. It will set the environment variables in the node.js environment, and the existing environment variable substitution in parcel should just work after that. I'd recommend doing this in the Bundler class immediately after startup.

Paths that we should load should follow this pattern: .env.${process.env.NODE_ENV}. We should load these paths: https://github.com/bkeepers/dotenv#what-other-env-files-can-i-use. That's what create-react-app does as well: https://github.com/facebookincubator/create-react-app/blob/master/packages/react-scripts/config/env.js

@devongovett devongovett changed the title Add basic .env support. [WIP] Add basic .env support. Dec 24, 2017
@devongovett devongovett changed the title [WIP] Add basic .env support. Add basic .env support. Jan 3, 2018
@devongovett devongovett merged commit 50de97f into parcel-bundler:master Jan 3, 2018
@devongovett
Copy link
Member

Thanks for your work on this. 👏

I refactored it slightly to move the code to a util file, and use the config resolution system to find .env files (searching upward from main file until they are found). Also added a test.

@cloud-walker
Copy link

cloud-walker commented Jan 7, 2018

this has not been npm published yet, right?

@DeMoorJasper
Copy link
Member

DeMoorJasper commented Jan 7, 2018

@cloud-walker It's already in the master branch but not on npm yet

@juliocanares
Copy link

waiting for this 🔥

@andywer
Copy link
Contributor

andywer commented Feb 15, 2018

As far as I can tell there is still no support for environment vars when using TypeScript, right? 😕

A distinct parcel-plugin-env might be helpful after all...

@luizfilipe
Copy link

This is already on npm? I'm also trying to read something regarding this on the docs, but there's no information about dotenv on it.

Can anyone help me here? I've place .env file at the same folder as entry file in this case index.html I'm using babel and I'm also using React for front-end I've set inside of .env file the environment variable that stores an URL like this API_URL=http://localhost:8081 and I'm doing a console.log on the script (<script src="src/index.js"></script>) that I'm calling on index.html.
But I'm still getting undefined.

Here is the js file looks like

'use strict';

import React from 'react';
import {render} from 'react-dom';
import {BrowserRouter as Router} from 'react-router-dom';
import {App} from './app'

console.log(process.env.API_URL);

render((
  <Router>
    <App/>
  </Router>
), document.getElementsByName('body'));

if (module.hot) {
  module.hot.accept();
}

@FDiskas
Copy link

FDiskas commented Mar 21, 2018

@luizfilipe place your .env file in root directory where package.json is located

@leriel
Copy link

leriel commented May 23, 2018

For anyone else that may come across this wondering - do not rely on process.env object to have values inside. Instead, rely directly on process.env.KEY as those are going to be replaced directly in the code, example:

.env

KEY=val

index.js

console.log(process.env.KEY); // val
console.log(process.env); // {}

DeMoorJasper pushed a commit to parcel-bundler/website that referenced this pull request Aug 24, 2018
This is my suggestion for documenting `.env` support parcel-bundler/parcel#258.

The original suggestion #216 was for this to be listed under the
>advanced section

but most of the advanced section is about authoring plugins/packagers and using the direct api. I thought keeping it alongside the usage explanation, namely post Code Splitting, a better fit.

I may raise an issue in the future to discuss reorganising the documentation as I've found it difficult for a number of reasons in the past.
devongovett pushed a commit that referenced this pull request Oct 15, 2018
* Add .env support.

* Handle promise rejection.

* Use process.env.DOTENV

* Use dotenv

* Use dotenv module

* Move env loading to a util, copy to workers, add tests
devongovett pushed a commit that referenced this pull request Oct 15, 2018
* Add .env support.

* Handle promise rejection.

* Use process.env.DOTENV

* Use dotenv

* Use dotenv module

* Move env loading to a util, copy to workers, add tests
@frankieshakes
Copy link

Can anyone help me here? I've place .env file at the same folder as entry file in this case index.html I'm using babel and I'm also using React for front-end I've set inside of .env file the environment variable that stores an URL like this API_URL=http://localhost:8081 and I'm doing a console.log on the script (<script src="src/index.js"></script>) that I'm calling on index.html.
But I'm still getting undefined.

I was experiencing the same thing. Restarting the Parcel server didn't seem to help. Running this command did the trick for me:

npx parcel --no-cache src/index.html

Hopefully this helps out the next person.

@risanto
Copy link

risanto commented Nov 9, 2019

npx parcel --no-cache src/index.html

This works wonder. Thanks a lot!

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

Successfully merging this pull request may close these issues.

None yet