0

I am trying to set the environment variable NODE_ENV for my project.

I am using Windows and have set the NODE_ENV in the system settings - this has been verified by typing SET and identifying for the row below in the output.

NODE_ENV=production

I cannot seem to get the variable to set in webpack though.

When adding the code below to my project (index.js) it only logs out undefined

console.log('PROCESS', process.env.NODE_ENV)

My webpack config:

const path = require('path');
const webpack = require('webpack');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const UglifyJSPlugin = require('uglifyjs-webpack-plugin')

process.env.NODE_ENV = process.env.NODE_ENV || 'development'

if (process.env.NODE_ENV === 'test') {
  require('dotenv').config({ path: '.env.test' })
} else if (process.env.NODE_ENV === 'development') {
  require('dotenv').config({ path: '.env.development' })
} else if (process.env.NODE_ENV === 'production') {
  require('dotenv').config({ path: '.env.production' })
} else {
  require('dotenv').config({ path: '.env.development' })
}

module.exports = (env) => {
  const isProduction = env === 'production';
  ...
    plugins: [
      CSSExtract,
      new UglifyJSPlugin(),
      new webpack.DefinePlugin({
        'process.env.FIREBASE_API_KEY': JSON.stringify(process.env.FIREBASE_API_KEY),
        'process.env.FIREBASE_AUTH_DOMAIN': JSON.stringify(process.env.FIREBASE_AUTH_DOMAIN),
        'process.env.FIREBASE_DATABASE_URL': JSON.stringify(process.env.FIREBASE_DATABASE_URL),
        ...
      }),
    devtool: isProduction ? 'source-map' : 'inline-source-map',
    ...

I have read this question, but still cannot get the env variable to set.

Where am I going wrong?

Kermit
  • 2,865
  • 3
  • 30
  • 53
  • is it accessible in `cmd` ? – pradosh nair Sep 17 '19 at 17:22
  • It's a bit more involved, but you could try using [Windows Subsystem for Linux](https://learn.microsoft.com/en-us/windows/wsl/about), which will then allow you to set environment variables easily like `NODE_ENV=development node foo.js`. It also solves a number of other compatibility issues with Windows. – Seth Holladay Sep 17 '19 at 17:31
  • Hi @pradoshnair - Yes the variable is accessible via the command prompt and the command prompt within VS Code (my code editor) by typing "set" – Kermit Sep 17 '19 at 17:59
  • Hi @SethHolladay - I use WSL a lot, but it integrates poorly into VS Code for me. I will try if packing for production in webpack works better i WSL. It does not solve the issue of being able to switch environment while coding, though. :/ – Kermit Sep 17 '19 at 18:01
  • @Kermit they released WSL 2 recently and VS Code receives updates frequently. Probably worth checking in on from time to time. – Seth Holladay Sep 25 '19 at 03:37

1 Answers1

0

I managed to get set the NODE_ENV by using the cross-env package! If you are developing node.js on Windows this can be very useful. Linux/Mac users would not have this problem.

To set the environment variable simply type

cross-env NODE_ENV=production [your commend goes here]

Example:

cross-env NODE_ENV=production webpack --env production

Kermit
  • 2,865
  • 3
  • 30
  • 53