0

This is my index.js file:

var express = require('express');
var app = express();
var compression = require('compression');
var path = require('path');

app.use(compression());
app.set('port', (process.env.PORT || 9000));

app.use(express.static(path.join(__dirname, 'dist')));
app.use('/public', express.static(path.join(__dirname, '/public')));

app.set('views', __dirname + '/');
app.set('view engine', 'ejs');

app.listen(app.get('port'), function() {
    console.log('Node app is running on port', app.get('port'));
});

And these are my NPM scripts for building and starting the server:

"scripts": {
    "start": "NODE_ENV=production node index.js",
    "dev": "NODE_ENV=development webpack-dev-server --progress --bail --open",
    "build": "NODE_ENV=production webpack -p --colors"
},

I don't know how but I still get ReferenceError: NODE_ENV is not defined. Can anyone help?

3
  • 2
    Where do you use NODE_ENV in your JavaScript code? (The code you've posted doesn't seem to me to be the relevant code.) Commented Mar 15, 2017 at 16:51
  • What OS are you using? Commented Mar 15, 2017 at 16:52
  • I'm using OS X Also whan i'm using webpack server there is no problem with that... I think this is not a problem with code it self. Commented Mar 15, 2017 at 16:55

2 Answers 2

4

You need to use:

process.env.NODE_ENV

instead of:

NODE_ENV

When you get the error:

ReferenceError: NODE_ENV is not defined

that means that you were trying to access it like it was a variable in scope and not a property on the process.env object as it is.

Sign up to request clarification or add additional context in comments.

3 Comments

where, how? in the index.js i assume ... I've tried this also but had the same issue :(
@Lukas I don't know where. Certainly not in the code that you provided because you didn't include anything with NODE_ENV
everywhere i have to in webpack config (using it to build app) i'm using process.env.NODE_ENV
0

It works on Mac.

What OS you used ? If you are Windows, maybe you need checkout cross-env

Then change your package.json like this:

{
  "scripts": {
    "build": "cross-env NODE_ENV=production webpack --config build/webpack.config.js"
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.