A secure webpack plugin that supports dotenv and other environment variables and only exposes what you choose and use.
Installations
npm install dotenv-webpack
Developer
mrsteele
Developer Guide
Module System
CommonJS, UMD
Min. Node Version
>=10
Typescript Support
No
Node Version
12.22.12
NPM Version
6.14.16
Statistics
1,295 Stars
481 Commits
76 Forks
8 Watching
10 Branches
23 Contributors
Updated on 09 Nov 2024
Bundle Size
4.63 kB
Minified
2.03 kB
Minified + Gzipped
Languages
JavaScript (100%)
Total Downloads
Cumulative downloads
Total Downloads
509,936,192
Last day
-2.2%
208,125
Compared to previous day
Last week
4.2%
1,092,019
Compared to previous week
Last month
13%
4,596,152
Compared to previous month
Last year
-20%
52,135,799
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dependencies
1
Peer Dependencies
1
dotenv-webpack
A secure webpack plugin that supports dotenv and other environment variables and only exposes what you choose and use.
Installation
Include the package locally in your repository.
npm install dotenv-webpack --save-dev
Description
dotenv-webpack
wraps dotenv
and Webpack.DefinePlugin
. As such, it does a text replace in the resulting bundle for any instances of process.env
.
Your .env
files can include sensitive information. Because of this,dotenv-webpack
will only expose environment variables that are explicitly referenced in your code to your final bundle.
Interested in taking your environments to the next level? Check out the Dotenv Organization.
Usage
The plugin can be installed with little-to-no configuration needed. Once installed, you can access the variables within your code using process.env
as you would with dotenv
.
The example bellow shows a standard use-case.
Create a .env file
1// .env 2DB_HOST=127.0.0.1 3DB_PASS=foobar 4S3_API=mysecretkey 5
Add it to your Webpack config file
1// webpack.config.js 2const Dotenv = require('dotenv-webpack'); 3 4module.exports = { 5 ... 6 plugins: [ 7 new Dotenv() 8 ] 9 ... 10};
Use in your code
1// file1.js 2console.log(process.env.DB_HOST); 3// '127.0.0.1'
Resulting bundle
1// bundle.js 2console.log('127.0.0.1');
Note: the .env
values for DB_PASS
and S3_API
are NOT present in our bundle, as they were never referenced (as process.env.[VAR_NAME]
) in the code.
How Secure?
By allowing you to define exactly where you are loading environment variables from and bundling only variables in your project that are explicitly referenced in your code, you can be sure that only what you need is included and you do not accidentally leak anything sensitive.
Recommended
Add .env
to your .gitignore
file
Limitations
Due to the fact that we use webpack.DefinePlugin
under the hood, we cannot support destructing as that breaks how this plugin is meant to be used. Because of this, please reference your variables without destructing. For more information about this, please review the issue here.
process.env
stubbing / replacing
process.env
is not polyfilled in Webpack 5+, leading to errors in environments where process
is null
(browsers).
We automatically replace any remaining process.env
s in these environments with "MISSING_ENV_VAR"
to avoid these errors.
When the prefix
option is set, process.env
s will not be stubbed.
If you are running into issues where you or another package you use interfaces with process.env
, it might be best to set ignoreStub: true
and make sure you always reference variables that exist within your code (See this issue for more information).
Properties
Use the following properties to configure your instance.
- path (
'./.env'
) - The path to your environment variables. This same path applies to the.env.example
and.env.defaults
files. Read more here. - safe (
false
) - If true, load '.env.example' to verify the '.env' variables are all set. Can also be a string to a different file. - allowEmptyValues (
false
) - Whether to allow empty strings in safe mode. If false, will throw an error if any env variables are empty (but only if safe mode is enabled). - systemvars (
false
) - Set to true if you would rather load all system variables as well (useful for CI purposes). - silent (
false
) - If true, all warnings will be suppressed. - expand (
false
) - Allows your variables to be "expanded" for reusability within your.env
file. - defaults (
false
) - Adds support fordotenv-defaults
. If set totrue
, uses./.env.defaults
. If a string, uses that location for a defaults file. Read more at npm. - ignoreStub (
false
) - Override the automatic check whether to stubprocess.env
. Read more here. - prefix (
'process.env.'
) - The prefix to use before the name of your env variables.
The following example shows how to set any/all arguments.
1module.exports = { 2 ... 3 plugins: [ 4 new Dotenv({ 5 path: './some.other.env', // load this now instead of the ones in '.env' 6 safe: true, // load '.env.example' to verify the '.env' variables are all set. Can also be a string to a different file. 7 allowEmptyValues: true, // allow empty variables (e.g. `FOO=`) (treat it as empty string, rather than missing) 8 systemvars: true, // load all the predefined 'process.env' variables which will trump anything local per dotenv specs. 9 silent: true, // hide any errors 10 defaults: false, // load '.env.defaults' as the default values if empty. 11 prefix: 'import.meta.env.' // reference your env variables as 'import.meta.env.ENV_VAR'. 12 }) 13 ] 14 ... 15};
About path
settings
As previously mentioned, it is possible to customize the path
where the .env
file is located as well as its filename from the plugin settings:
1module.exports = { 2 ... 3 plugins: [ 4 new Dotenv({ 5 path: './some.other.env', 6 }) 7 ] 8 ... 9};
It is important to mention that this same path and filename will be used for the location of the .env.example
and .env.defaults
files if they are configured, this will only add the .example
and .defaults
suffixes respectively:
1module.exports = { 2 ... 3 plugins: [ 4 new Dotenv({ 5 path: '../../path/to/other.env', 6 safe: true, // load '../../path/to/other.env.example' 7 defaults: true, // load '../../path/to/other.env.defaults' 8 }) 9 ] 10 ... 11};
This is especially useful when working with Monorepos where the same configuration can be shared within all sub-packages of the repository:
1. 2├── packages/ 3│ ├── app/ 4│ │ └── webpack.config.js # { path: '../../.env' } 5│ └── libs/ 6│ └── webpack.config.js # { path: '../../.env' } 7├── .env 8├── .env.example 9└── .env.defaults
LICENSE
MIT
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
no dangerous workflow patterns detected
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
Found 3/12 approved changesets -- score normalized to 2
Reason
0 commit(s) and 3 issue activity found in the last 90 days -- score normalized to 2
Reason
detected GitHub workflow tokens with excessive permissions
Details
- Warn: no topLevel permission defined: .github/workflows/main.yml:1
- Info: no jobLevel write permissions found
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/main.yml:52: update your workflow using https://app.stepsecurity.io/secureworkflow/mrsteele/dotenv-webpack/main.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/main.yml:56: update your workflow using https://app.stepsecurity.io/secureworkflow/mrsteele/dotenv-webpack/main.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/main.yml:60: update your workflow using https://app.stepsecurity.io/secureworkflow/mrsteele/dotenv-webpack/main.yml/master?enable=pin
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/main.yml:78: update your workflow using https://app.stepsecurity.io/secureworkflow/mrsteele/dotenv-webpack/main.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/main.yml:82: update your workflow using https://app.stepsecurity.io/secureworkflow/mrsteele/dotenv-webpack/main.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/main.yml:86: update your workflow using https://app.stepsecurity.io/secureworkflow/mrsteele/dotenv-webpack/main.yml/master?enable=pin
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/main.yml:92: update your workflow using https://app.stepsecurity.io/secureworkflow/mrsteele/dotenv-webpack/main.yml/master?enable=pin
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/main.yml:13: update your workflow using https://app.stepsecurity.io/secureworkflow/mrsteele/dotenv-webpack/main.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/main.yml:17: update your workflow using https://app.stepsecurity.io/secureworkflow/mrsteele/dotenv-webpack/main.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/main.yml:21: update your workflow using https://app.stepsecurity.io/secureworkflow/mrsteele/dotenv-webpack/main.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/main.yml:24: update your workflow using https://app.stepsecurity.io/secureworkflow/mrsteele/dotenv-webpack/main.yml/master?enable=pin
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/main.yml:27: update your workflow using https://app.stepsecurity.io/secureworkflow/mrsteele/dotenv-webpack/main.yml/master?enable=pin
- Warn: npmCommand not pinned by hash: .github/workflows/main.yml:27
- Warn: npmCommand not pinned by hash: .github/workflows/main.yml:65
- Warn: npmCommand not pinned by hash: .github/workflows/main.yml:91
- Info: 0 out of 7 GitHub-owned GitHubAction dependencies pinned
- Info: 0 out of 5 third-party GitHubAction dependencies pinned
- Info: 0 out of 3 npmCommand dependencies pinned
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 24 are checked with a SAST tool
Reason
14 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-67hx-6x53-jw92
- Warn: Project is vulnerable to: GHSA-grv7-fg5c-xmjg
- Warn: Project is vulnerable to: GHSA-3xgq-45jj-v275
- Warn: Project is vulnerable to: GHSA-9c47-m6qq-7p4h
- Warn: Project is vulnerable to: GHSA-952p-6rrq-rcjv
- Warn: Project is vulnerable to: GHSA-f8q6-p94x-37v3
- Warn: Project is vulnerable to: GHSA-9wv6-86v2-598j
- Warn: Project is vulnerable to: GHSA-c2qf-rxjj-qqgw
- Warn: Project is vulnerable to: GHSA-mxhp-79qh-mcx6
- Warn: Project is vulnerable to: GHSA-72xf-g2v4-qvf3
- Warn: Project is vulnerable to: GHSA-hc6q-2mpp-qw7j
- Warn: Project is vulnerable to: GHSA-4vvj-4cpr-p986
- Warn: Project is vulnerable to: GHSA-j8xg-fqg3-53r7
- Warn: Project is vulnerable to: GHSA-3h5v-q93c-6h6q
Score
3.2
/10
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More