I can set 'NODE_ENV' on an npm script the linux way on one windows laptop, but not the other. I want to understand why

huangapple go评论65阅读模式
英文:

I can set 'NODE_ENV' on an npm script the linux way on one windows laptop, but not the other. I want to understand why

问题

我只是尝试理解我的上一台Windows笔记本和新笔记本的设置差异。

注意:

  • 旧笔记本运行Windows 10,新笔记本运行Windows 11。
  • 在两者中都使用git bash。
  • 在两者中都启用了WSL功能(不确定是否有关紧要)。
  • nvm控制了两者中的Node版本。
  • 运行的npm脚本定义为:NODE_ENV=production gulp build
  • 旧笔记本上没有安装cross-envwin-node-env或类似的全局或项目中的包。
  • 在任何一台机器上都没有在Windows PATH环境变量中发现可能会影响此问题的内容。

旧笔记本正常工作,新笔记本报错:'NODE_ENV' 不是内部或外部命令,可执行的程序或批处理文件

为什么会在一台机器上发生这种情况而在另一台机器上没有?我还可以检查什么?我愿意使用cross-env,而且可能会这样做,但我真的只是想了解。我愿意提供更多详细信息。

英文:

I am just trying to understand the delta between the setup of my last windows laptop and my new one.

Notes:

  • OLD runs windows 10, NEW runs windows 11.
  • using git bash in both.
  • WSL enabled in features in both (not sure if this matters at all).
  • nvm controls node version in both.
  • running an npm script defined as: NODE_ENV=production gulp build.
  • no cross-env, win-node-env or similar packages installed on old one, globally or in the project.
  • nothing in the windows PATH environment variable on either that would seem to affect this.

> OLD works fine, NEW gives the error: 'NODE_ENV' is not recognized as an internal or external command, operable program or batch file.

Why would this happen on one and not the other? What else can I check? Happy to use cross-env and probably will, but I really just want to understand. Happy to provide any more details.

答案1

得分: 0

Okay I figured it out - 原来npm在运行脚本时使用了cmd,尽管我是在git bash中运行命令

解决方法很简单,将bash设置为脚本shell,问题就解决了。不需要使用cross-env(或其他任何规范化工具),所有开发者都可以使用相同的脚本,而不受其操作系统的影响。

设置npm脚本shell:

npm config set shell "C:\\Program Files\\Git\\bin\\bash.exe"
npm config set script-shell "C:\\Program Files\\Git\\bin\\bash.exe"

这将在您的.npmrc文件中添加以下条目:

script-shell=C:\Program Files\Git\bin\bash.exe
shell=C:\Program Files\Git\bin\bash.exe

也许关闭并重新打开您的终端是个好主意。

英文:

Okay I figured it out - It turns out that npm was using cmd for its script shell, even though I am running the command in git bash.

The fix is quite simple, set bash as the script shell and voila. No need for cross-env (or any other normalizer for that matter), and all of your developers can use the same scripts regardless of their OS.

Set the npm script shell:

npm config set shell "C:\\Program Files\\Git\\bin\\bash.exe"
npm config set script-shell "C:\\Program Files\\Git\\bin\\bash.exe"

This will add entries to your .npmrc file as:

script-shell=C:\Program Files\Git\bin\bash.exe
shell=C:\Program Files\Git\bin\bash.exe

May be a good idea to close and reopen your terminal.

huangapple
  • 本文由 发表于 2023年7月18日 00:05:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76706279.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定