Reputation: 649
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'",
}
Upvotes: 23
Views: 18439
Reputation: 2974
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",
}
Upvotes: 10
Reputation: 86
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:
Upvotes: 0
Reputation: 47471
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"
Upvotes: 9
Reputation: 792
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.
Upvotes: 1