为什么 `DEBUG=foo node index.js` 在 `package.json` 的 `scripts` 部分失败
Why does `DEBUG=foo node index.js` fails in `scripts` section of `package.json`
我在 Windows 10,但我正在使用 git bash
。当 运行ning 来自 git bash
以下内容时:
DEBUG=foo node index.js
它工作正常并设置了环境变量DEBUG
。但是如果我把它放到 package.json
:
的 scripts
部分
"scripts": {
"start": "DEBUG=foo node index.js"
},
和 运行 以下来自 git bash
我得到以下错误:
$ npm start
> nodejs@1.0.0 start C:\Users\maksym.koretskyi\Desktop\nodejs
> DEBUG=foo node index.js
'DEBUG' is not recognized as an internal or external command,
operable program or batch file.
为什么会这样?
您似乎 运行在 Windows 上使用它。
假设你Bash像你说的那样工作,我会制作一个脚本:
#!/bin/bash
DEBUG=foo node index.js
例如run-debug
并使用:
"scripts": {
"start": "bash run-debug"
},
in package.json
以确保 DEBUG=foo node index.js
命令由 Bash 解释而不是 command.com 或 [=39= 中的 shell ] 叫做。参见 Issue #6543: npm scripts shell select。
如果您希望它 运行 即使在没有 Bash 的系统上,为了最大的跨平台兼容性,最好使用 Node 脚本而不是 shell脚本:
"scripts": {
"start": "node run-debug.js"
},
在这种情况下,run-debug.js
可能包含如下内容:
let env = Object.create(process.env);
env.DEBUG = 'foo';
spawn('node', ['app.js'], {env});
另请参阅:
是的,确实,我发现了以下 thread 并且它指出:
The shell config variable is only used by the npm explore command,
which forks a subshell. It's not meant to be used to allow you to use,
say, bash on Windows, or switch between Powershell and cmd.exe.
Scripts are always run by the shebang line on Unix, and cmd.exe on
Windows.
看来这是设计使然,无法更改。
我在 Windows 10,但我正在使用 git bash
。当 运行ning 来自 git bash
以下内容时:
DEBUG=foo node index.js
它工作正常并设置了环境变量DEBUG
。但是如果我把它放到 package.json
:
scripts
部分
"scripts": {
"start": "DEBUG=foo node index.js"
},
和 运行 以下来自 git bash
我得到以下错误:
$ npm start
> nodejs@1.0.0 start C:\Users\maksym.koretskyi\Desktop\nodejs
> DEBUG=foo node index.js
'DEBUG' is not recognized as an internal or external command,
operable program or batch file.
为什么会这样?
您似乎 运行在 Windows 上使用它。
假设你Bash像你说的那样工作,我会制作一个脚本:
#!/bin/bash
DEBUG=foo node index.js
例如run-debug
并使用:
"scripts": {
"start": "bash run-debug"
},
in package.json
以确保 DEBUG=foo node index.js
命令由 Bash 解释而不是 command.com 或 [=39= 中的 shell ] 叫做。参见 Issue #6543: npm scripts shell select。
如果您希望它 运行 即使在没有 Bash 的系统上,为了最大的跨平台兼容性,最好使用 Node 脚本而不是 shell脚本:
"scripts": {
"start": "node run-debug.js"
},
在这种情况下,run-debug.js
可能包含如下内容:
let env = Object.create(process.env);
env.DEBUG = 'foo';
spawn('node', ['app.js'], {env});
另请参阅:
是的,确实,我发现了以下 thread 并且它指出:
The shell config variable is only used by the npm explore command, which forks a subshell. It's not meant to be used to allow you to use, say, bash on Windows, or switch between Powershell and cmd.exe. Scripts are always run by the shebang line on Unix, and cmd.exe on Windows.
看来这是设计使然,无法更改。