为什么DEBUG = foo节点index.js在package.json的scripts部分失败了 [英] Why does `DEBUG=foo node index.js` fails in `scripts` section of `package.json`
问题描述
我在Windows 10上,但我正在使用git bash
.从git bash
运行时,以下内容:
I'm on Windows 10, but I'm using git bash
. When running from git bash
the following:
DEBUG=foo node index.js
它可以正常工作并设置环境变量DEBUG
.但是,如果我将其放入package.json
的scripts
部分:
it works fine and sets the environment variable DEBUG
. But if I put it into the scripts
section of the package.json
:
"scripts": {
"start": "DEBUG=foo node index.js"
},
并从git bash
运行以下命令,我得到以下错误:
and run the following from git bash
I get the following error:
$ 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上运行它.
It seems like you're running it on Windows.
假设您按照您所说的操作Bash,我将编写一个脚本:
Assuming that you have Bash working as you said, I would make a script:
#!/bin/bash
DEBUG=foo node index.js
例如run-debug
并使用:
"scripts": {
"start": "bash run-debug"
},
,以确保Bash解释了 in 对于某些情况,即使您希望它甚至在没有Bash的系统上运行,也要获得最大的跨平台兼容性,最好使用Node脚本而不是Shell脚本: For cases when you want it to run even on systems without Bash, for maximum cross-platform compatibility, it's even better to use a Node script instead of a shell script: 在这种情况下, In this case the 另请参阅: 这篇关于为什么DEBUG = foo节点index.js在package.json的scripts部分失败了的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!DEBUG=foo node index.js
命令,而不是command.com或Windows中的任何shell被解释.参见问题#6543:npm脚本shell选择.package.json
to make sure that the DEBUG=foo node index.js
command is interpreted by Bash and not command.com or whatever the shell in Windows is called. See Issue #6543: npm scripts shell select."scripts": {
"start": "node run-debug.js"
},
run-debug.js
可能包含以下内容:run-debug.js
could contain something like:let env = Object.create(process.env);
env.DEBUG = 'foo';
spawn('node', ['app.js'], {env});