Why does `DEBUG=foo node index.js` fails in `scripts` section of `package.json`

血红的双手。 提交于 2019-12-02 01:37:55
rsp

It seems like you're running it on Windows.

Assuming that you have Bash working as you said, I would make a script:

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

called e.g. run-debug and use:

"scripts": {
    "start": "bash run-debug"
},

in 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.

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:

"scripts": {
    "start": "node run-debug.js"
},

In this case the run-debug.js could contain something like:

let env = Object.create(process.env);
env.DEBUG = 'foo';
spawn('node', ['app.js'], {env});

See also:

Yes, indeed, I've found the following thread and it states that:

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.

So it seems that's by design and there's no way to change it.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!