Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

npm: How to set NODE_ENV in Windows (10)?

Tags:

npm

I am trying to add an npm script in package.json that sets NODE_ENV before doing something else (like running webpack). But although the syntax seems to be correct, NODE_ENV is not set when running under Windows 10.

Test script

"scripts": {
   "test": "SET NODE_ENV=debug && echo %NODE_ENV%" }

The result from npm run test is "production" (provided NODE_ENV was set to "production" before running the script). Should be "debug".

What could be wrong? I even tried cross-env with no success.

Edit

To clarify my question: I cannot set any environment variable under Windows 10. And I need to call SET because I am running the script under Windows (10). Seems to be some rights problem (scripts not allowed to set environment variables?).

Another (or the actual) question would be: How can I create one script to build (using webpack) with creating minified versions of JavaScript files (for production), and one script to create non-minified versions (for development). So far I use following approach (see comments for the important parts):

Edit 2

I did not now that this probably made a difference, but in case it does: I worked with an React app created with create-react-app. I found the answer to my question, see below.

package.json:

{
  "name": "test",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {

    // Scipts for build for development and for production
    "build-dev": "SET NODE_ENV=debug webpack",
    "build-release": "SET NODE_ENV=production webpack"

  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "babel-core": "^6.24.1",
    "babel-loader": "^7.0.0",
    "babel-preset-env": "^1.4.0",
    "babel-preset-react": "^6.24.1",
    "debug": "^2.6.4",
    "webpack": "^2.4.1"
  }
}

webpack.config.js:

const path = require('path');
var webpack = require('webpack');

// Check if in debug environment
var debug = process.env.NODE_ENV !== "production";

module.exports = {
  context: path.join(__dirname, 'src'),
  entry: ['./index.js'],
  output: {
    path: path.join(__dirname, 'www/js'),
    filename: 'index.js',
  },
  devtool: 'source-map',
  module: {
    rules: [
      {
        test: /\.jsx?$/,
        exclude: /node_modules/,
        use: ['babel-loader'],
      }],
  },

  // Add the UglifyJs plugin only in debug mode
  plugins: debug ? []  : [new webpack.optimize.UglifyJsPlugin({ mangle: false, sourcemap: false })],

  resolve: {
    modules: [
      path.join(__dirname, 'node_modules')
    ]
  }
};

This fails because setting NODE_ENV does not work for some reason. Using the command prompt directly like in the scripts:

SET NODE_ENV = debug
webpack

works by the way. That's proof that the configuration is okay, but just the npm script cannot set NODE_ENV.

like image 543
Jürgen Bayer Avatar asked Apr 24 '17 11:04

Jürgen Bayer


People also ask

How do I change environment variables in Windows 10 NPM?

"Edit the System environment variables" option will be popped in the result. Open that, select the "Path" and click on edit, then click "New" add your nodeJS Bin path i.e in my machine its installed in c:\programfiles\nodejs\node_modules\npm\bin. Once you added click "Ok" then close.

How do I set an environment variable in Windows?

On the Windows taskbar, right-click the Windows icon and select System. In the Settings window, under Related Settings, click Advanced system settings. On the Advanced tab, click Environment Variables. Click New to create a new environment variable.

How do you set a node environment to production?

You can signal Node. js that you are running in production by setting the NODE_ENV=production environment variable. in the shell, but it's better to put it in your shell configuration file (e.g. . bash_profile with the Bash shell) because otherwise the setting does not persist in case of a system restart.


2 Answers

Just in case you are STILL having issues setting the NODE_ENV in Windows 10 - this will help you. In your package.json file add the following:

"test": "SET \"NODE_ENV=test\""

If you plan on pushing this to Heroku - you will have to "export" the variable and your string would look like this (you are escaping the Windows-NEEDED quotes with a slash):

"test": "export NODE_ENV=test || SET \"NODE_ENV=test\""

Lastly, if you need a following command like mocha then the line would look like this:

"test": "export NODE_ENV=test || SET \"NODE_ENV=test\" && mocha server/**/*.name_of_files_plus_test.js"

Hope this helps someone :) - Mike

like image 86
Michael Carolan Avatar answered Oct 14 '22 02:10

Michael Carolan


I found the answer to my question in the meantime, basically in the create-react-app readme: Firstly in an app created with create-react-app NODE_ENV cannot be manually overridden. Secondly, when setting environment variables, their name must start with "REACT_APP_". This was the solution for me.

In package.json:

"scripts": {
    ...
    "build:staging": "SET REACT_APP_ENVIRONMENT=Staging && npm run build"
}

In the code:

if (process.env.REACT_APP_ENVIRONMENT === "Staging") ...
like image 24
Jürgen Bayer Avatar answered Oct 14 '22 02:10

Jürgen Bayer