node.js NODE_ENV 未被识别为内部或外部命令
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40034690/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
NODE_ENV is not recognised as an internal or external command
提问by Shubham Khatri
I am developing in node.js and wanted to take into account both production and development environment. I found out that setting NODE_ENV while running the node.js server does the job. However when I try to set it in package.json script it gives me the error:
我正在 node.js 中开发,并希望同时考虑生产和开发环境。我发现在运行 node.js 服务器时设置 NODE_ENV 可以完成这项工作。但是,当我尝试在 package.json 脚本中设置它时,它给了我错误:
NODE_ENV is not recognised as an internal or external command
NODE_ENV 未被识别为内部或外部命令
Below is my package.json
下面是我的 package.json
{
"name": "NODEAPT",
"version": "0.0.0",
"private": true,
"scripts": {
"start": "NODE_ENV=development node ./bin/server",
"qa2": "NODE_ENV=qa2 node ./bin/server",
"prod": "NODE_ENV=production node ./bin/server"
},
"dependencies": {
"body-parser": "~1.15.1",
"cookie-parser": "~1.4.3",
"debug": "~2.2.0",
"express": "~4.13.4",
"fs": "0.0.1-security",
"jade": "~1.11.0",
"morgan": "~1.7.0",
"oracledb": "^1.11.0",
"path": "^0.12.7",
"serve-favicon": "~2.3.0"
}
}
I run my node server as: npm run qa2for example.
我运行我的节点服务器:npm run qa2例如。
I don't know what I am doing wrong. Any help is appreciated
我不知道我做错了什么。任何帮助表示赞赏
回答by Jagrati
Since you are using windows operating system., the command varies from the unix system command that you are using.
由于您使用的是windows operating system.,因此该命令与您使用的 unix 系统命令不同。
In windows you have to modify you script as.
在 Windows 中,您必须将脚本修改为。
"scripts": {
"start": " SET NODE_ENV=development & node ./bin/server",
"qa2": "SET NODE_ENV=qa2 & node ./bin/server",
"prod": "SET NODE_ENV=production & node ./bin/server"
},
Use SETand then an &after that.
使用SET然后在&之后。
However using cross-envnpm package for cross platform stability is recommeded.
但是建议使用cross-envnpm 包来实现跨平台稳定性。
Install it like npm install -S cross-env
安装它像 npm install -S cross-env
"scripts": {
"start": " cross-env NODE_ENV=development & node ./bin/server",
"qa2": "cross-env NODE_ENV=qa2 & node ./bin/server",
"prod": "cross-env NODE_ENV=production & node ./bin/server"
},
回答by Lazyexpert
I can suggest cross platform sollution. It's done with the help of the cross-envnpm package. Your script section would look like this:
我可以建议跨平台解决方案。它是在cross-envnpm 包的帮助下完成的。您的脚本部分将如下所示:
"scripts": {
"globals" : "npm i -g cross-env",
"start": "cross-env NODE_ENV=development & node ./bin/server",
"qa2": "cross-env NODE_ENV=qa2 & node ./bin/server",
"prod": "cross-env NODE_ENV=production & node ./bin/server"
}
So you run once:
所以你运行一次:
npm run globals // to install global dependencies
Then you're free to use your scripts both on linux and windows(mac?).
然后你可以在 linux 和 windows(mac?) 上自由使用你的脚本。

