Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

npm multiple command execution && is not working but & and || does

I have been trying to run multiple npm commands to run some of my cypress tests in a sequence. after some research what I found was to use && in between npm commands.

package.json file, inside scripts I defined a test as,

"scripts":{
"test":"npm run cypresscommand_1 && npm run cypresscommand_2"
}

When I execute this using npm run test for some reason, npm run cypresscommand_1 got executed but npm run cypresscommand_2 didn't get executed. With further research and going through some tutorials, later I tried two modifications

modification 1: insted of && I used &

"scripts":{
"test":"npm run cypresscommand_1 & npm run cypresscommand_2"
}

modification 2: insted of && I used ||

"scripts":{
"test":"npm run cypresscommand_1 || npm run cypresscommand_2"
}

surprisingly both gave me the expected results which mean both executed npm run cypresscommand_1 and then npm run cypresscommand_2

What I want to know is,

  1. In the new versions of npm did they replace with && with & and ||
  2. Is the meaning of & is equal to and
  3. Is the meaning of || is equal to and
  4. Is there any difference between & and ||

Although the code is working perfectly, I want to make sure if I'm using the correct syntax or not. Can someone help?

Thank you.

like image 374
Muditha Perera Avatar asked Oct 24 '25 00:10

Muditha Perera


1 Answers

It has nothing to do with npm, these are interpreted as bash commands on Linux based systems,

& - means it will run as background job,

&& - exit code from each command and uses it as an operand in a chained && operation.

| - is a pipe operator where the output of one command is passed on to the following command,

|| - the OR logical operator, and make Bash continue processing chained commands if only one of a pair completes.

For your case you can try using semicolon ';' which runs the commands one after other even if it fails.

"scripts":{ "test":"npm run cypresscommand_1 ; npm run cypresscommand_2" }

like image 173
Hrudayanath Avatar answered Oct 26 '25 23:10

Hrudayanath



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!