Application 2017-09-26

Using dotEnv Environment Variables on the Client Side - dotenv-webpack

Master dotenv-webpack to load environment variables on the client side with webpack and security best practices.

Read in: ja
Using dotEnv Environment Variables on the Client Side - dotenv-webpack

dotenv-webpack is a useful library when you want to use environment variables prepared in a .env file on the client side.

Introduction

npm install dotenv-webpack --save-dev

Set it as a plugin in webpack.config.js.

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

module.exports = [
  ~~~ゴニョゴニョゴニョ~~~
  {
    plugins: [new Dotenv({
        path: 'path/to/.env',
        safe: false
      })]
  }
  ~~~ゴニョゴニョゴニョ~~~
];

path sets the path to the .env file, and safe determines whether to load .env_example.

Usage

DOMAIN=hereisyourdomain
config.log(process.env.DOMAIN) // hereisyourdomain

Thoughts

It's convenient, but is there no security issue?

References

Tags: JavaScript npm webpack
Share: 𝕏 Post Facebook Hatena
✏️ View source / Discuss on GitHub
☕ Support

If you enjoy this blog, consider supporting it. Every bit helps keep it running!


Related Articles