为什么在`package.json`的`scripts`部分中`DEBUG = foo node index.js`失败

时间:2016-12-29 11:47:21

标签: node.js bash package.json

我在Windows 10上,但我使用的是git bash。从以下git bash开始运行时:

DEBUG=foo node index.js

它工作正常并设置环境变量DEBUG。但是,如果我将其放入scripts的{​​{1}}部分:

package.json

并从 "scripts": { "start": "DEBUG=foo node index.js" }, 运行以下内容我收到以下错误:

git bash

为什么会这样做?

2 个答案:

答案 0 :(得分:3)

好像你在Windows上运行它。

假设你按照说法让Bash工作,我会制作一个剧本:

#!/bin/bash
DEBUG=foo node index.js

称为例如run-debug并使用:

"scripts": {
    "start": "bash run-debug"
},
<{1>}中的

以确保package.json命令由Bash解释,而不是command.com或Windows中的任何shell被调用。请参阅Issue #6543: npm scripts shell select

如果您希望它在没有Bash的系统上运行,为了获得最大的跨平台兼容性,最好使用Node脚本而不是shell脚本:

DEBUG=foo node index.js

在这种情况下,"scripts": { "start": "node run-debug.js" }, 可能包含以下内容:

run-debug.js

另见:

答案 1 :(得分:1)

是的,确实,我发现了以下thread并声明:

  

shell配置变量仅由npm explore命令使用,   这是一个子壳。它并不意味着用于允许您使用,   比如说,在Windows上使用bash,或者在Powershell和cmd.exe之间切换。   脚本总是由Unix上的shebang行运行,cmd.exe运行   视窗

所以它似乎是设计上的,而且无法改变它。