π’ Node Emulation
Some features that ultimately emulate Node.js's API
ΒΆ π³ Environment Variables
Parcel uses dotenv to support loading environment variables from .env
files.
.env
files are to be stored alongside the package.json
that contains your parcel-bundler
dependency.
Parcel loads .env
files with these specific names for the following NODE_ENV
values:
valid .env filenames |
NODE_ENV=* |
NODE_ENV=test |
---|---|---|
.env |
βοΈ | βοΈ |
.env.local |
βοΈ | βοΈ |
.env.${NODE_ENV} |
βοΈ | βοΈ |
.env.${NODE_ENV}.local |
βοΈ | βοΈ |
Notably:
NODE_ENV
defaults todevelopment
..env.local
is not loaded whenNODE_ENV=test
since tests should produce the same results for everyone- Sometimes introducing a new .env file will not work immediately. Try deleting the .cache/ directory in this case.
- Accessing the
process.env
object directly is not supported, but accessing specific variables on it likeprocess.env.API_KEY
will provide the expected value. - Use the built-in
process
Node.js global, i.e. don't doimport process from "process"
. If you use TypeScript, you probably want to install@types/node
for it to compile.
ΒΆ π³οΈ Polyfilling & Excluding Builtin Node Modules
When (or more likely a dependency) importing packages such as crypto
, fs
or process
, Parcel will either automatically use one of the listed polyfills and otherwise exclude that module. You can use the alises
field in your package.json
native module | npm replacement | native module | npm replacement |
---|---|---|---|
assert | assert |
process | process/browser.js |
buffer | buffer |
punycode | punycode |
console | console-browserify |
querystring | querystring-es3 |
constants | constants-browserify |
stream | readable-stream |
crypto | crypto-browserify |
string_decoder | string_decoder |
domain | domain-browser |
sys | util/util.js |
events | events |
timers | timers-browserify |
http | stream-http |
tty | tty-browserify |
https | https-browserify |
url | url |
os | os-browserify/browser.js |
util | util/util.js |
path | path-browserify |
vm | vm-browserify |
zlib | browserify-zlib |
ΒΆ π Inlining fs.readFileSync
Calls to fs.readFileSync
are replaced with the file's contents if the filepath is statically determinable
fs.readFileSync(..., "utf8")
: with the contants as string with (or any other valid encoding)fs.readFileSync(...)
: a Buffer object (e.g.Buffer.from(....)
together with the an potentionally necessary polyfill)