node.js “NODE_ENV”不是内部或外部命令、可操作命令或批处理文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11928013/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 16:09:12  来源:igfitidea点击:

"NODE_ENV" is not recognized as an internal or external command, operable command or batch file

windowsnode.js

提问by krozero

I'm trying to setup an environment for a Node.js app. but I'm getting this error every time.

我正在尝试为 Node.js 应用程序设置环境。但我每次都会收到这个错误。

"NODE_ENV" is not recognized as an internal or external command, operable command or batch file.

“NODE_ENV”不是内部或外部命令、可操作命令或批处理文件。

What does this mean and how can I solve this problem?

这是什么意思,我该如何解决这个问题?

I'm using Windows and also tried set NODE_ENV=developmentbut had no luck.

我正在使用 Windows,也尝试过set NODE_ENV=development但没有运气。

回答by Jim O'Neil

It sounds like your error comes from an attempt to run something like this (which works in Linux)

听起来您的错误来自尝试运行这样的东西(在 Linux 中有效)

NODE_ENV=development node foo.js

the equivalent in Windows would be

Windows 中的等效项是

SET NODE_ENV=development
node foo.js

running in the same command shell. You mentioned set NODE_ENV did not work, but wasn't clear how/when you executed it.

在同一个命令 shell 中运行。您提到 set NODE_ENV 不起作用,但不清楚您如何/何时执行它。

回答by laggingreflex

I wrote a module for this: win-node-env.

我为此编写了一个模块:win-node-env

It creates a NODE_ENV.cmdthat sets the NODE_ENVenvironment variable and spawns a child process with the rest of the command and its args.

它创建一个NODE_ENV.cmd设置NODE_ENV环境变量并使用其余命令及其参数生成子进程的。

Just install it (globally), and run your npm script commands, it should automatically make them work.

只需安装它(全局),然后运行您的 npm 脚本命令,它就会自动使它们工作。

npm install -g win-node-env

回答by Mahmudul Hasan

for windows use &in between command also. Like,

对于 windows&在命令之间也使用。喜欢,

  "scripts": {
    "start": "SET NODE_ENV=development & nodemon app/app.js",
  }

回答by Susan-stack

  1. npm install "cross-env"module.
  2. modify the code as cross-env NODE_ENV=development node foo.js. Then you can run the like npm run build.
  1. npm install "cross-env"模块。
  2. 将代码修改为cross-env NODE_ENV=development node foo.js. 然后你可以运行类似的npm run build

回答by AmerllicA

Use win-node-env, For using it just run below command on your cmdor power shellor git bash:

使用共赢节点ENV,对于使用它只是在你运行下面的命令cmdpower shellgit bash

npm install -g win-node-env

After it everything is like Linux.

在它之后,一切都像 Linux。

回答by Flion

set NODE_ENV=production & nodemon app/app.js

will cause NODE_ENV to contain a spaceat the end:

将导致 NODE_ENV在末尾包含一个空格

process.env.NODE_ENV == 'production'; //false
process.env.NODE_ENV == 'production '; //true

As mentioned in a comment here, use this instead:

作为在评论中提到这里,用这个来代替:

NODE_ENV=production&& nodemon app/app.js

回答by Jon Crowell

Changing your scripts to accommodate Windows is a royal pain. Trying to figure out the appropriate Windows translations and maintaining 2 sets of scripts is no way to live your life.

更改脚本以适应 Windows 是一种极大的痛苦。试图找出合适的 Windows 翻译并维护 2 套脚本是没有办法过上你的生活的。

It's much easier to configure npm to use bash on Windows and your scripts will run as is.

将 npm 配置为在 Windows 上使用 bash 要容易得多,并且您的脚本将按原样运行。

Simply run npm config set script-shell "C:\\Program Files\\Git\\bin\\bash.exe". Make sure the path to the bash executable is correct for your machine. You'll likely need to start a new instance of the terminal for the change to take effect.

只需运行npm config set script-shell "C:\\Program Files\\Git\\bin\\bash.exe"。确保 bash 可执行文件的路径对于您的机器是正确的。您可能需要启动一个新的终端实例才能使更改生效。

The screenshot below illustrates the benefit.

下面的屏幕截图说明了好处。

  1. npm ERR! when trying to run script initially.
  2. Script modified for Windows use runs but doesn't show the return message.
  3. After updating npm config to use bash, the script runs and returns the appropriate message.
  1. 错误!最初尝试运行脚本时。
  2. 为 Windows 使用修改的脚本运行但不显示返回消息。
  3. 更新 npm config 以使用 bash 后,脚本将运行并返回相应的消息。

Getting npm scripts to run as is in Windows

让 npm 脚本在 Windows 中按原样运行

回答by user3790180

For those who uses Git Bash and having issues with npm run <script>,

对于那些使用 Git Bash 并遇到问题的人npm run <script>

Just set npm to use Git Bash to run scripts

只需将 npm 设置为使用 Git Bash 来运行脚本

npm config set script-shell "C:\\Program Files\\git\\bin\\bash.exe"(change the path according to your installation)

npm config set script-shell "C:\\Program Files\\git\\bin\\bash.exe"(根据您的安装更改路径)

And then npm will run scripts with Git Bash, so such usages like NODE_ENV=will work properly.

然后 npm 将使用 Git Bash 运行脚本,所以像这样的用法NODE_ENV=会正常工作。

回答by Bang Andre

npm install -S cross-env

Worked for me

对我来说有效

回答by SirPhemmiey

Most of the answers up there didn't help me..

那里的大多数答案都没有帮助我..

What helped me was NODE_ENV=production&& nodemon app/app.js

帮助我的是 NODE_ENV=production&& nodemon app/app.js

Take note of the space. Good luck.

注意空间。祝你好运。