19

What I want to do is when I run npm run dev to execute those both tasks I need to wait for 5 seconds until next task runs.

Run the npm run server wait for 5 seconds and then npm run client

  "scripts": {
    "start": "node ./bin/www",
    "server": "nodemon start",
    "client": "gulp",
    "dev": "concurrently 'npm run server' 'npm run client'", 
}
Matt
  • 609
  • 2
  • 12
  • 19

6 Answers6

22

Assuming you're on Linux, you can use sleep command:

"dev": "concurrently 'npm run server' 'sleep 5 && npm run client'"
TGrif
  • 5,725
  • 9
  • 31
  • 52
  • Hi, thank you for the answers. I am using macOS but others members of the team are using Windows, and the server obviously NodeJs. For some reason, the second task runs at the same time that first task, which gives me an error because I need to fully load the first task first. – Matt Oct 04 '17 at 19:55
  • So maybe _concurrently_ is not the right tool for this (since it is precisely its main task) and you can go with something like `"dev": "npm run server && npm run client"`. (can't help much for windows) – TGrif Oct 04 '17 at 20:30
7

Use Sleep and Background tasks.

Depending on what these tasks do, you might not need concurrently. You might be able to get away with just using & to run one of them as a background task.

The general pattern is as follows:

( sleep 5 && delayed_command ) & immediate_command

So in your case, it would look something like this:

"dev": "( sleep 5 && npm run client ) & npm run server"

If you don't want the output from one of the commands, you can add > /dev/null 2>&1 after the command, like this:

"dev": "( sleep 5 && npm run client > /dev/null 2>&1 ) & npm run server"
Joshua Pinter
  • 45,245
  • 23
  • 243
  • 245
5

Cross platform solution for your particular problem, without installing any additional lib. Just invoking a JS script that waits certain time to end.

"scripts": {
  "start": "node ./bin/www",
  "server": "nodemon start",
  "client": "gulp",
  "sleep5s": "node -e \"setTimeout(() => process.exit(0), 5000)\"",
  "delayed-client": "npm run sleep5s && npm run client",
  "dev": "concurrently npm:server npm:delayed-client",
}
José Antonio Postigo
  • 2,674
  • 24
  • 17
1

Adding to @TGrif answer, chain scripts with double ampersand && to execute them sequntially. So to execute sleep command first, put && after it and then write npm run client. So the second command(npm run client) will execute only when the first(sleep) is complete.

Avantika Saini
  • 792
  • 4
  • 9
  • For those using Windows and Powershell like me, https://stackoverflow.com/questions/55330065/how-to-run-multiple-powershell-commands-in-scripts-for-package-json#answer-55330147 is an absolutely-invaluable resource! ;-) – Kenny83 Aug 12 '21 at 21:05
1
node -e "setTimeout(()=>{},5000)" && npm run client
A Code Cow
  • 45
  • 1
  • 5
  • 1
    While this code snippet may solve the problem, it doesn't explain why or how it answers the question. Please [include an explanation for your code](//meta.stackexchange.com/q/114762/269535), as that really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – Luca Kiebel Feb 22 '22 at 18:11
0

To add onto Littleboy Harry's answer, sleep 5 only works on Linux. For a cross-platform solution, you can use node -e "setTimeout(()=>{},5000)". So you'd want to make your package.json:

"scripts": {
"start": "node ./bin/www",
"server": "nodemon start",
"client": "gulp",
"dev": "npm run server && node -e \"setTimeout(()=>{}, 5000)\" && npm run client", 
}

A couple of points:

  • I added a backslash before the double quotes as an escape sequence since the command takes the script in double quotes too.
  • You can adjust the timeout according to approximately how long it takes to start the server. Right now, it's set to 5000ms (5 seconds).