Load a specific file and its env vars as actual Node process env vars (in process.env):
1
require('dotenv').config({path:'.env'})
The following allows to load multiple env files according to an ENV_FILE and APP_RUNTIME_ENV env var,
and will filter out the env vars you'd like to expose to your app to the one defined in the env files
(excluding other process env vars):
constDotEnv=require('dotenv');constdotenvFilesBasePath='./'// Load env vars from .env.* files in processconstenvVarsKeys=[process.env.ENV_FILE,`.env.${process.env.APP_RUNTIME_ENV}.local`,`.env.${process.env.APP_RUNTIME_ENV}`,'.env.local','.env',].filter(Boolean).reduce((keys,path)=>{if(require('fs').existsSync(dotenvFilesBasePath+path)){constparsedConfig=DotEnv.config({path:dotenvFilesBasePath+path});keys.push(...Object.keys(parsedConfig.parsed));}returnkeys;},['NODE_ENV']);// Filter out allowed env vars (those defined in the .env files)constenvVars=Object.fromEntries(Object.entries(process.env).filter(([key])=>envVarsKeys.includes(key))// Optionnaly, exclude some built-time specific vars used for Webpack (prefixed with WEBPACK_):.filter(([key])=>!key.startsWith('WEBPACK_')));
Note
Adapt the dotenvFilesBasetPath for your env files according to your project.
For instance, in a Symfony Webpack Encore projet, your frontend dedicated .env
files might be located inside assets/.
In previous versions of Webpack, we used to expose the process.env object directly to our frontend applications.
It used to work because Webpack 4 was included polyfills for some Node.js variables in the browser.
But as of Webpack 5, it's now unavailable by default.
webpack 5 does no longer include a polyfill for this Node.js variable. Avoid using it in the frontend code.
Want to use environment variables with process.env.VARIABLE?
Consider using VARIABLE instead. process.env is Node.js specific and should be avoided in frontend code.
However, you can make these variables accessible to your web application using
an AppConfig object and the Webpack DefinePlugin plugin.
const{DefinePlugin}=require('webpack');module.exports=(env,argv)=>{return{// […]plugins:[// […]newDefinePlugin({AppConfig:JSON.stringify({NODE_ENV:argv.mode,// default NODE_ENV value to mode value if not provided explicitly through env...envVars,// allowed env vars from env files or their actual process value if provided during build}),}),],// […]};}
In your Makefile:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
## Build assetsbuild:exportAPP_RUNTIME_ENV ?= developmentbuild:
npx webpack --mode=development
## Build assets in staging modebuild@staging:exportAPP_RUNTIME_ENV ?= stagingbuild@staging:
npx webpack --mode=production
## Build assets in production modebuild@production:exportAPP_RUNTIME_ENV ?= productionbuild@production:
npx webpack --mode=production
varEncore=require('@symfony/webpack-encore');varwebpack=require('webpack');Encore.configureDefinePlugin(options=>({AppConfig:JSON.stringify({NODE_ENV:process.env.NODE_ENV||'development',// default NODE_ENV value if not provided explicitly through env...envVars,// allowed env vars from env files or their actual process value if provided during build})}));
In your Makefile:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
## Build assetsbuild:exportAPP_RUNTIME_ENV ?= developmentbuild:
npx encore dev
## Build assets in staging modebuild@staging:exportAPP_RUNTIME_ENV ?= stagingbuild@staging:
npx encore production
## Build assets in production modebuild@production:exportAPP_RUNTIME_ENV ?= productionbuild@production:
npx encore production
Define global env vars go to the .env file, and specific development/production/staging env vars
to .env.development, .env.production and .env.staging files.
Tip
You could also load a specific .env.local file (or any other file) using ENV_FILE=.env.local make build