Parcel: Is there any way to use env variables?

Created on 6 Jun 2018  Β·  11Comments  Β·  Source: parcel-bundler/parcel

❔ Question

Hi Guys 😸

I would like to use Environment Variables in my Parcel application.

Webpack offers a plugin EnvironmentPlugin

Parcel offers something similar? ❀️

πŸ”¦ Context

use env variables with Parcel :)

πŸ’» Code Sample

that plugin allows me to do this in my webpack application
40679652-61ae9244-6349-11e8-96bb-0f29a6b7130b

🌍 Your Environment

| Software | Version(s) |
| ---------------- | ---------- |
| Parcel | 1.8.1
| Node | 10.3.0
| npm/Yarn | 1.7.0
| Operating System | macOS Hig Sierra(10.13.5)

Question

Most helpful comment

Parcel endorses standardised formats, as we don’t want to make the users dependent of one tool like webpack does with it’s custom config that contains everything. Therefore a custom .json config will be unlikely

Sent with GitHawk

All 11 comments

We support environment files natively

Sent with GitHawk

@DeMoorJasper Hi πŸ‘‹ guide on how to use it? I'm interested in using it in the same way that I can use it in webpack.

It would be great if you could give me an example πŸ™

@MAPESO just drop your .env file in your project root directory. Parcel has built in support for dotenv

your-project-directory
  |-- src
  .env
  package.json
  yarn.lock

@minhchu I need to use files with a .json extension :(

Why do you need that :thinking: In the end, you still want to get your process.env.TOKEN. You can achieve the same thing by using dotenv :D

Parcel endorses standardised formats, as we don’t want to make the users dependent of one tool like webpack does with it’s custom config that contains everything. Therefore a custom .json config will be unlikely

Sent with GitHawk

@DeMoorJasper @minhchu thank you for clarifying the doubt 😸

I'm going to close the issue :)

I get this error

module.js:549
    throw err;
    ^

Error: Cannot find module 'dotenv-expand'
    at Function.Module._resolveFilename (module.js:547:15)
    at Function.Module._load (module.js:474:25)
    at Module.require (module.js:596:17)
    at require (internal/module.js:11:18)
    at dotenvFiles.forEach.dotenvFile (/Users/bogdansoare/Hacking/design-system/node_modules/react-scripts-parcel/config/env.js:41:5)
    at Array.forEach (<anonymous>)
    at Object.<anonymous> (/Users/bogdansoare/Hacking/design-system/node_modules/react-scripts-parcel/config/env.js:39:13)
    at Module._compile (module.js:652:30)
    at Object.Module._extensions..js (module.js:663:10)
    at Module.load (module.js:565:32)
error Command failed with exit code 1.

Hi, is it possible to inject env vars in html files?

@dumptyd I was asking the same question and came up with the following solution using posthtml + posthtml-expressions https://github.com/parcel-bundler/parcel/issues/1209#issuecomment-432424397

Was this page helpful?
0 / 5 - 0 ratings

Related issues

devongovett picture devongovett  Β·  3Comments

Niggler picture Niggler  Β·  3Comments

philipodev picture philipodev  Β·  3Comments

davidnagli picture davidnagli  Β·  3Comments

medhatdawoud picture medhatdawoud  Β·  3Comments