0

I have a Node.js application. My build script in my package.json looks like this:

"build": "webpack --config webpack.prod.js && set NODE_ENV=production&& npm run express-server"

When I run this script locally on my computer, I can access the process.env.NODE_ENV and it sure enough is in "production"

But when I do the same thing on my Ubuntu server hosted on digital ocean, then process.env.NODE_ENV is 'undefined'.

How can I set the NODE_ENV in a npm script on a Ubuntu server?

Roi
  • 983
  • 9
  • 17

1 Answers1

1

Edit:

  1. According to this answer it says:

    In UBUNTU use:

    $ export NODE_ENV=test

    Note, the use of export instead of set.


Original answer:

  1. Firstly try changing your build script to:

    "build": "webpack --config webpack.prod.js && NODE_ENV=\"production\" npm run express-server"
    

    Note the set keyword has been omitted, escaped double quotes have been wrapped around \"production\", and the && operator has been removed.

  2. If that also fails then checkout cross-env. It's documentation states:

    Most Windows command prompts will choke when you set environment variables with NODE_ENV=production like that. (The exception is Bash on Windows, which uses native Bash.) Similarly, there's a difference in how windows and POSIX commands utilize environment variables. With POSIX, you use: $ENV_VAR and on windows you use %ENV_VAR%.

    So perhaps try utilizing cross-env and change your build script to the following:

    "build": "webpack --config webpack.prod.js && cross-env NODE_ENV=\"production\" npm run express-server"
    
RobC
  • 22,977
  • 20
  • 73
  • 80