简体   繁体   English

ReferenceError:NODE_ENV未使用Express定义

[英]ReferenceError: NODE_ENV is not defined with Express

This is my index.js file: 这是我的index.js文件:

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: 这些是我用于构建和启动服务器的NPM脚本:

"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 . 我不知道如何,但是我仍然收到ReferenceError: NODE_ENV is not defined Can anyone help? 有人可以帮忙吗?

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. 这意味着您正在尝试访问它,就像它是作用域中的变量而不是process.env对象的属性一样。

It works on Mac. 它适用于Mac。

What OS you used ? 您使用了什么操作系统? If you are Windows, maybe you need checkout cross-env 如果您使用的是Windows,则可能需要跨环境结帐

Then change your package.json like this: 然后像这样更改package.json:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM