Socket
Socket
Sign inDemoInstall

dotenv-webpack

Package Overview
Dependencies
293
Maintainers
1
Versions
45
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    dotenv-webpack

A simple webpack plugin to support dotenv.


Version published
Weekly downloads
825K
decreased by-20.37%
Maintainers
1
Install size
43.4 kB
Created
Weekly downloads
 

Package description

What is dotenv-webpack?

The dotenv-webpack npm package is a plugin for webpack that automates the loading of environment variables from a .env file into process.env, providing an easy way to configure your application's environment-specific variables. It wraps dotenv and Webpack.DefinePlugin under the hood.

What are dotenv-webpack's main functionalities?

Load environment variables

Automatically loads environment variables from a .env file and makes them available in your application through process.env.

const Dotenv = require('dotenv-webpack');

module.exports = {
  //... other webpack config
  plugins: [
    new Dotenv()
  ]
  //... other webpack config
};

Custom .env file path

Allows you to specify a custom path to your .env file if it's not located in the root directory.

const Dotenv = require('dotenv-webpack');

module.exports = {
  //... other webpack config
  plugins: [
    new Dotenv({
      path: './some/other/path/.env'
    })
  ]
  //... other webpack config
};

Safe mode

Enables safe mode to ensure that all necessary environment variables are defined, by comparing with a template file (usually .env.example).

const Dotenv = require('dotenv-webpack');

module.exports = {
  //... other webpack config
  plugins: [
    new Dotenv({
      safe: true // load '.env.example' to check all necessary variables are defined
    })
  ]
  //... other webpack config
};

System variables

Allows the inclusion of system environment variables in the webpack build in addition to the variables from the .env file.

const Dotenv = require('dotenv-webpack');

module.exports = {
  //... other webpack config
  plugins: [
    new Dotenv({
      systemvars: true // load all system environment variables in addition to .env file
    })
  ]
  //... other webpack config
};

Other packages similar to dotenv-webpack

Readme

Source

A secure webpack plugin that supports dotenv and other environment variables and only exposes what you choose and use.

npm Travis Dependency Status devDependency Status

dotenv webpack dotenv-webpack

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.

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
// .env
DB_HOST=127.0.0.1
DB_PASS=foobar
S3_API=mysecretkey

Add it to your Webpack config file
// webpack.config.js
const Dotenv = require('dotenv-webpack');

module.exports = {
  ...
  plugins: [
    new Dotenv()
  ]
  ...
};
Use in your code
// file1.js
console.log(process.env.DB_HOST);
// '127.0.0.1'
Resulting bundle
// bundle.js
console.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.

Add .env to your .gitignore file

Properties

Use the following properties to configure your instance.

  • path ('./.env') - The path to your environment variables.
  • 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 for dotenv-defaults. If set to true, uses ./.env.defaults. If a string, uses that location for a defaults file. Read more at https://www.npmjs.com/package/dotenv-defaults.

The following example shows how to set any/all arguments.

module.exports = {
  ...
  plugins: [
    new Dotenv({
      path: './some.other.env', // load this now instead of the ones in '.env'
      safe: true, // load '.env.example' to verify the '.env' variables are all set. Can also be a string to a different file.
      allowEmptyValues: true, // allow empty variables (e.g. `FOO=`) (treat it as empty string, rather than missing)
      systemvars: true, // load all the predefined 'process.env' variables which will trump anything local per dotenv specs.
      silent: true, // hide any errors
      defaults: false // load '.env.defaults' as the default values if empty.
    })
  ]
  ...
};

LICENSE

MIT

Keywords

FAQs

Last updated on 11 May 2020

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc