Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run multiple PowerShell commands with package.json scripts

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)
like image 409
Evandro Pomatti Avatar asked Aug 14 '26 23:08

Evandro Pomatti


1 Answers

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.

like image 189
Evandro Pomatti Avatar answered Aug 16 '26 17:08

Evandro Pomatti



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!