DEV Community

Cover image for Using dotEnv Environment Variables on the Client Side - dotenv-webpack
Kenta Takeuchi
Kenta Takeuchi

Posted on • Originally published at bmf-tech.com

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

This article was originally published on bmf-tech.com.

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
      })]
  }
  ~~~ゴニョゴニョゴニョ~~~
];

Enter fullscreen mode Exit fullscreen mode

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

Usage

DOMAIN=hereisyourdomain
Enter fullscreen mode Exit fullscreen mode
config.log(process.env.DOMAIN) // hereisyourdomain
Enter fullscreen mode Exit fullscreen mode

Thoughts

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

References

Top comments (0)