8

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.

codejockie
  • 9,020
  • 4
  • 40
  • 46
Jürgen Bayer
  • 2,993
  • 3
  • 26
  • 51

3 Answers3

11

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

codejockie
  • 9,020
  • 4
  • 40
  • 46
Michael Carolan
  • 195
  • 2
  • 7
2

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") ...
codejockie
  • 9,020
  • 4
  • 40
  • 46
Jürgen Bayer
  • 2,993
  • 3
  • 26
  • 51
-1

Did you try?

set DEBUG=* & npm run test

Make sure debug already installed

npm install debug --save

UPDATE: To set environment variable in windows use

set NODE_ENV=dev //for development environment

In your case

"scripts": {
   "test": "NODE_ENV=dev && echo %NODE_ENV%" }
Jyothi Babu Araja
  • 10,076
  • 3
  • 31
  • 38
  • I don't understand wha debug has to do with my question. I want to set NODE_ENV to debug or production in a build script that calls webpack after, to be able to minify the scripts in production, and not minify them for debuging. – Jürgen Bayer Apr 24 '17 at 11:58
  • Yes you can set `NODE_ENV` to `dev` and also set a variable `DEBUG=your_debugger_name` to use your NODE debugger. – Jyothi Babu Araja Apr 24 '17 at 12:00
  • I don't need to use a debugger. And I cannot set NODE_ENV to some value, that's my problem. – Jürgen Bayer Apr 24 '17 at 12:01
  • Actually `NODE_ENV` comprises either `production` or `dev` but not like `debug` as any Environment. – Jyothi Babu Araja Apr 24 '17 at 12:03
  • I think we have a misunderstanding here: I have the problem, that setting environment varibles (any as I tried) does not work (under Windows 10). Your last edit would not work under Windows 10 anyway, because setting environment variables requires SET. Here cross-env would come into play, but this does not work either. I think I may have a problem with some tights (scripts not allowed to set environment variables or something). Thanks anyway. – Jürgen Bayer Apr 24 '17 at 12:08