3

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:

> [email protected] 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! [email protected] debug-windows-test: `$env:NODE_ENV="dev"; node src/dequeue.js`  
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] 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)

1 Answer 1

2

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 [email protected] you can customize the shell with the script-shell configuration.

Sign up to request clarification or add additional context in comments.

2 Comments

For a cross-platform solution (i.e. for windows cmd and *nix sh) utilize cross-env.
@RobC I didn't know about that one! I will certainly try it out for package.json and other automations.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.