2

I have the following script definition "debug-windows" in my package.json:

{
    "scripts": {
        "debug-windows": "$env:NODE_ENV=\"dev\"; node src/dequeue.js"
    }
}

Which I run using npm run debug-windows and I get the error:

> servicebus-timeout@1.0.0 debug-windows C:\myapp
> $env:NODE_ENV="dev"; node src/dequeue.js

The filename, directory name, or volume label syntax is incorrect.
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! servicebus-timeout@1.0.0 debug-windows-test: `$env:NODE_ENV="dev"; node src/dequeue.js`  
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the servicebus-timeout@1.0.0 debug-windows-test script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\<USERNAME>\AppData\Roaming\npm-cache\_logs\2020-04-27T16_29_32_585Z-debug.log

If I run the same command directly on PowerShell it succeeds:

PS C:\myapp> $env:NODE_ENV="dev"; node src/dequeue.js
Waiting for messages: Mon Apr 27 2020 13:24:06 GMT-0300 (Brasilia Standard Time)
Evandro Pomatti
  • 13,341
  • 16
  • 97
  • 165

1 Answers1

1

I spent some time checking the npm docs and found out the problem. npm-run-script runs CMD on Windows, and not PowerShell.

The solution was to separate the commands using & and the appropriate set to define session variables:

"debug-windows": "set NODE_ENV=dev & node src/dequeue.js"

I have no need for npm to use PowerShell, it's just that I thought it was doing so.

If one wants to run PowerShell for other reasons, check this thread which has a lot of options.

Documentation on npm-run-script:

The actual shell your script is run within is platform dependent. By default, on Unix-like systems it is the /bin/sh command, on Windows it is the cmd.exe. The actual shell referred to by /bin/sh also depends on the system. As of npm@5.1.0 you can customize the shell with the script-shell configuration.

Evandro Pomatti
  • 13,341
  • 16
  • 97
  • 165
  • For a cross-platform solution (i.e. for windows `cmd` and _*nix_ `sh`) utilize [cross-env](https://www.npmjs.com/package/cross-env). – RobC Apr 28 '20 at 10:09
  • @RobC I didn't know about that one! I will certainly try it out for `package.json` and other automations. – Evandro Pomatti Apr 28 '20 at 11:37