4

Having a strange issue here with npm (on Windows, if it matters).

Chaining two commands with && works fine:

"test:integration": "firebase emulators:exec --non-interactive \"npm run test:integration:runner\" && kill-port 9000",

But if I substitute ; for && it fails to work:

"test:integration": "firebase emulators:exec --non-interactive \"npm run test:integration:runner\" ; kill-port 9000",

Instead of running the first command successfully, npm run test:integration just returns immediately if I use ;.

I want to run kill-port 9000 as cleanup after the first command, regardless of the exit status from the first command, which is why I switched to ;.

Any idea what's going on here? Is there an alternative way I can do this besides using ;?

(I did get it working by chanining && and || like (first_command && kill-port 9000) || kill-port 9000, but that seems hackish and I would like to understand why ; isn't working)

3
  • The shell npm utilizes to run npm scripts on Win is cmd by default. However on *nix (Linux, macOS, ...) it utilizes sh as the default shell to run npm scripts. As you’re using Win, (essentially using cmd when running npm scripts), then consider substituting the semicolon (;) with the AND operator, i.e. a single ampersand (&). Note: the AND operator in cmd (&) has the same logic as the semicolon (;) in sh on *nix - which is essentially execute command 2 regardless of whether command 1 fails or succeeds. Commented Jul 18, 2022 at 9:24
  • However, be mindful that the AND operator (&) has a different meaning/logic in sh on *nix. Commented Jul 18, 2022 at 9:33
  • Ooh, that's interesting, but I definitely don't like how & is system dependent. It's surprising that it wasn't abstracted by nom. Anyhow, if you post this as an answer I'll gladly accept it. Commented Jul 18, 2022 at 10:22

1 Answer 1

4

The shell that npm utilizes to run npm scripts on Windows is cmd by default. However on *nix (Linux, macOS, ...) it utilizes sh as the default shell to run npm scripts.

As you’re using Windows, (essentially using cmd when running npm scripts), then consider substituting the semicolon (;) with the AND operator, i.e. a single ampersand (&).

The AND operator in cmd (&) has the same logic as the semicolon (;) in sh on *nix - which is essentially execute command 2 regardless of whether command 1 fails or succeeds.

Notes:

  1. Be mindful that the AND operator (&) has a different meaning/logic in sh on *nix.
  2. FYI, I answered a similar question here.
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks again! Powershell supports ;, and it looks like it's possible to change the shell npm uses on WIndows (stackoverflow.com/questions/23243353/…), but I think I'm going to stick with my || and && composition for now.
You’re welcome... yes that’s wise,(first_command && kill-port 9000) || kill-port 9000 yields the desired result with sh or cmd regardless of how "hackish" it may feel.

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.