Running NPM scripts sequentially

138,378

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

Share:
138,378

Related videos on Youtube

Rice
Author by

Rice

Updated on September 23, 2021

Comments

  • Rice
    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
      Tzach Ovadia almost 5 years
      Updated answer at the bottom
    • Tzach Ovadia
      Tzach Ovadia over 4 years
      According the documentation of Start command, you should be able to use /wait parameter (Start application and wait for it to terminate)
  • Rice
    Rice over 7 years
    I would require them to wait for each other to finish, these will fire off sequentially but won't wait.
  • dvlsg
    dvlsg over 7 years
    I don't think this is a node / npm problem. The start command you are executing in windows technically is finished. Use the /wait flag with start to force the start application to remain open until the internal method is also finished.
  • dvlsg
    dvlsg over 7 years
    To 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 run start /wait notepad. The start 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
    Rice over 7 years
    The \wait solution does not work for commands appended via double ampersand or pre/post fixed npm commands.
  • Rice
    Rice over 7 years
    This 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
    Bernhard Döbler about 6 years
    && are evaluated by the shell and don't work on Windows.
  • Alberto S.
    Alberto S. about 6 years
    Lets 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
    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
    Rafe Goldberg almost 6 years
    This 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
    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
    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
    VPaul almost 5 years
    How can we chain npm i with something? I tried and it failed as it considers && as a package.
  • Mobiletainment
    Mobiletainment almost 5 years
    @Rishinder just list all the packages you want to install separated by a whitespace: npm i package1 package2 package3
  • VPaul
    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
    Russell Chisholm over 4 years
    this 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
    Robert about 4 years
    This is the way to go. npm-run-all is the best. And will produce a package.json that is cross-platform.
  • Qwerty
    Qwerty over 3 years
    @VPaul Shouldn't you perhaps use postinstall script for that?
  • Qwerty
    Qwerty over 3 years
    I 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
    Vahid over 3 years
    On 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 my package.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
    Cheetara over 3 years
    Coming here from an issue I face - "killall node && next dev" why doesn't this run sequentially? (> killall node && next dev....Terminated)
  • dgo
    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
    sydd about 3 years
    While 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 the left-pad issue.
  • bFunc
    bFunc over 2 years
    as 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
    TamusJRoyce over 2 years
    OS specific, doesn't work on windows
  • Aerodynamic
    Aerodynamic over 2 years
    That 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
    TamusJRoyce over 2 years
    the 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
    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
    Günter Zöchbauer over 2 years
    Calling 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
    Merc almost 2 years
    Do I see this correctly, that run-s is really just replacing npm 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 uses run-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
    Gil Epshtain almost 2 years
    I 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 and clean (for example) then I prefer to use the && syntax.