2

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

DEBUG=foo node index.js

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"
  },

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.

Why the behaviour?

Max Koretskyi
  • 101,079
  • 60
  • 333
  • 488
  • 2
    Your error comes as a result of an instruction executed under a `Windows` environment (note the `batch` keyword) and not the `bash` shell. – Inian Dec 29 '16 at 12:06
  • thanks, that's what I thought. Do you know why the `DEBUG=foo node index.js` is executed under Windows env even if I run `npm start` from `git bash`? – Max Koretskyi Dec 29 '16 at 12:10
  • Can you try re-affirming once again, if `node` is installed peroperly, may be checking `$PATH` in `Git bash` to see if it has the path where node is installed – Inian Dec 29 '16 at 12:15
  • it should have, since `DEBUG=foo node index.js` is executed fine from `git bash` – Max Koretskyi Dec 29 '16 at 12:19

2 Answers2

3

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:

Community
  • 1
  • 1
rsp
  • 107,747
  • 29
  • 201
  • 177
2

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.

Max Koretskyi
  • 101,079
  • 60
  • 333
  • 488