Running NPM scripts sequentially
Solution 1
Invoke these scripts via npm run and chain them with double ampersand &&
:
npm run pre-build && npm run build_logic && npm run post_build && npm run exit
Explanation:
- Use
&&
(double ampersand) for sequential execution. - Use
&
(single ampersand) for parallel execution.
Solution 2
Following @Mobiletainment's great answer, you can also use npm-run-all to make the command much shorter and much more readable. In your case:
"scripts": {
...
"build": "run-s pre-build build_logic post_build exit"
}
run-s
is a shortcut npm-run-all
provides, that runs all the given npm-scripts sequentially, hence the -s
(run-s
is a shorter version of npm-run-all -s
).
Solution 3
You can prefix your scripts pre
and post
so they will execute automatically:
"scripts": {
"prebuild": "echo \"Welcome\" && exit 1",
"build": "start cmd.exe @cmd /k \"yo esri-appbuilder-js:widget && exit 1\"",
"postbuild": "start C:\\WebAppBuilderForArcGIS\\startupShortcut",
"exit" : "start cmd.exe @cmd /k \"echo \"goodbye\" && exit 1\""
}
then run npm run build
Solution 4
You could just string them into another script. "start": "pre-build && build_logic && post_build && exit"
Solution 5
You can use npm-run-all to combine multiple commands in a lot of different ways
For example, if you had the following scripts in your package.json
:
"scripts": {
"clean": "rimraf dist",
"lint": "eslint src",
"build": "babel src -o lib"
}
You could run them all sequentially like this:
$ npm-run-all clean lint build
See this question for how to run multiple npm commands in parallel
Related videos on Youtube
Rice
Updated on September 23, 2021Comments
-
Rice over 2 years
Let's say I have
"scripts": { "pre-build": "echo \"Welcome\" && exit 1", "build_logic": "start cmd.exe @cmd /k \"yo esri-appbuilder-js:widget && exit 1\"", "post_build": "start C:\\WebAppBuilderForArcGIS\\startupShortcut", "exit" : "start cmd.exe @cmd /k \"echo \"goodbye\" && exit 1\"" },
What NPM command can I run to let all of these scripts launch sequentially. When I use pre/post fixing they launch sequentially but they don't wait for the parent script to finish before executing. I am assuming the only solution is like: How do I get Gulp tasks to fire sequentially when firing shell commands in an async.series helper function? ? I know this can be done with Gulp but I would like to stick with NPM for now to explore its capabilities. Thanks for any help!
-
Tzach Ovadia almost 5 yearsUpdated answer at the bottom
-
Tzach Ovadia over 4 yearsAccording the documentation of
Start
command, you should be able to use/wait
parameter (Start application and wait for it to terminate)
-
-
Rice over 7 yearsI would require them to wait for each other to finish, these will fire off sequentially but won't wait.
-
dvlsg over 7 yearsI don't think this is a node / npm problem. The
start
command you are executing in windows technically is finished. Use the/wait
flag withstart
to force thestart
application to remain open until the internal method is also finished. -
dvlsg over 7 yearsTo test this -- run
start notepad
from a command prompt, then take a look at the original command prompt. You should be able to type in another command. Then runstart /wait notepad
. Thestart
command should continue to "run" while your notepad window is open (take a look at the command prompt again). Then once you close notepad,start
will be finished. -
Rice over 7 yearsThe \wait solution does not work for commands appended via double ampersand or pre/post fixed npm commands.
-
Rice over 7 yearsThis works the best because it executes in order, being that each command doesn't execute until the previos is finished, as requested in the OP. Thanks
-
Bernhard Döbler about 6 years
&&
are evaluated by the shell and don't work on Windows. -
Alberto S. about 6 yearsLets say that I have two scripts within my app. One launch a live server (script1), and another one a dyson server (script2). How I can create a script that triggers those two previous scripts? thanks :)
-
aruno about 6 years@BernhardDöbler just add another npm command containing the above command - you don't have to run (and remember) this whole string from windows CLI.
-
Rafe Goldberg almost 6 yearsThis should not be the accepted answer. Per @BernhardDöbler, the
&&
syntax is a UNIX construct. It will behave incorrectly on Window's machines, with potentially breaking consequences for your build process. -
Rice almost 6 years@RafeGoldberg The && operator has the same execution behavior in both windows and *nix environments, i.e. sequential execution..Unless there's something we're all missing?
-
Rafe Goldberg almost 6 years@Rice oy vey; was being dumb and mixing up my single and double-ampersand operators. Per the
npm-run-all
docs: “we sometimes use&
to run multiple command in parallel, but Windows'cmd.exe
... does not support [this operator].” So it'd appear you were right — at least from my brief research the&&
operator seems perfectly cross-platform compatible. -
VPaul almost 5 yearsHow can we chain
npm i
with something? I tried and it failed as it considers && as a package. -
Mobiletainment almost 5 years@Rishinder just list all the packages you want to install separated by a whitespace:
npm i package1 package2 package3
-
VPaul almost 5 years@Mobiletainment, that's not what I was trying to achieve. I wanted to execute other non installation scripts after
npm i
but it doesn't let me chain those. I've figured out a workaround but not very satisfied with it. -
Russell Chisholm over 4 yearsthis doesn't work, as indicated in the question: "When I use pre/post fixing they launch sequentially but they don't wait for the parent script to finish before executing."
-
Robert about 4 yearsThis is the way to go. npm-run-all is the best. And will produce a package.json that is cross-platform.
-
Qwerty over 3 years@VPaul Shouldn't you perhaps use
postinstall
script for that? -
Qwerty over 3 yearsI have read that the only difference between
&
and&&
is that&&
performs error checking and will not run commands to the right, if the commands on the left don't return expected result. MS:KB Archive -
Vahid over 3 yearsOn Windows 10 the
&&
works for me. I have a ghost blog which my next js app connects to it. So, I use this command in mypackage.json
file to run both the blog and the next js sequentially:"scripts": { "all": "cd my-blog-folder && ghost start && cd.. && next dev", ... }
. Then I run this command to execute that:npm run-script all
-
Cheetara over 3 yearsComing here from an issue I face - "killall node && next dev" why doesn't this run sequentially? (> killall node && next dev....Terminated)
-
dgo about 3 years@Qwerty - This is for sure true on Windows, the single
&
may be context sensitive on unix and do other things . -
sydd about 3 yearsWhile this seems like an elegant solution at first, checking out the dependency tree of
npm-run-all
reveals that it has 69 dependencies (not counting dev deps!), see e.g. npm.broofa.com/?q=npm-run-all . No thanks, I dont want to be a victim of some kind of sideload attack or a farce like theleft-pad
issue. -
bFunc over 2 yearsas was mentioned && will not work in Windows shell
&& are evaluated by the shell and don't work on Windows
, however it will work in git bash shell (automatically installed by git) or if you need it in regular cmd, than simple install of github.com/bmatzelle/gow utils will do the thing -
TamusJRoyce over 2 yearsOS specific, doesn't work on windows
-
Aerodynamic over 2 yearsThat is not necessarily true and depends on your windows version and the shell that you use. Check the comments here (and maybe best use a unix shell): stackoverflow.com/a/39172660/5037146
-
TamusJRoyce over 2 yearsthe point of npm is to be cross-shell/platform. otherwise you should stick with .sh, .bat, .makefile, cmake, sln, ... - Concurrently is an npm option. && is cross platform (seems to work even in powershell, even though running directly fails). & is not
-
TamusJRoyce over 2 years&& works for me on windows shell. However single & does not. Concurrently or an inline node script is the correct way to run commands in parallel.
-
Günter Zöchbauer over 2 yearsCalling out to another script worked for me instead of
&
(ignore exit code)"my-script": "npm run some-command-with-non-0-exit-code && do-something-else"
-
Merc almost 2 yearsDo I see this correctly, that
run-s
is really just replacingnpm run pre-build && npm run build_logic && npm run post_build && npm run exit
? why would I introduce another dependency, that confuses every developer, who knows the&&
command? In my case I am working on project that usesrun-s
(introduced by another dev) and it confuses me rather than helping me! Can I remove run-s securely or am I missing a difference here? -
Gil Epshtain almost 2 yearsI think that, if you have a lot of scripts to run, and you are using Package.json as a Task Runner (like Grunt or Gulp) then npm-run-all may be a valid tool. However, if you want to run only two scripts like
build
andclean
(for example) then I prefer to use the&&
syntax.