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

前端 未结 2 1628
后悔当初
后悔当初 2021-01-22 13:20

I\'m on Windows 10, but I\'m using git bash. When running from git bash the following:

DEBUG=foo node index.js

it wor

2条回答
  •  执笔经年
    2021-01-22 14:01

    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:

    • NPM script under cygwin/windows: the syntax of the command is incorrect

提交回复
热议问题