I have my package.json start like this
"scripts": {
"start": "react-scripts start",
How to add also “json-server db.json” to start ?
I have my package.json start like this
"scripts": {
"start": "react-scripts start",
How to add also “json-server db.json” to start ?
Here are four different ways to do that.
Still I dont understand how to use it. I tried to write it like that
"scripts": {
"start": "json-server db.json && react-scripts start",
but the main application at localhost:3000 does not load. How to I should write this ?
That command will wait for the json-server db.json
command to finish before running the react-scripts start
command, which is not what you want.
What happens when you use only one ampersand?
"scripts": {
"start": "json-server db.json & react-scripts start",
If that doesn’t work, have a look at the Concurrently package, which allows you to run multiple commands concurrently.
Yes I would definitely go with concurrently, most importantly for better process handling. If you just run a command in the background using a single &
, and the foreground process (i.e. the last one in the line) dies, the background process will keep running which raises several problems:
stdout
and clutter your shell prompt (or wherever you redirected the output to)To fix this, you’d have to find and kill the background processes manually then. Conversely, if a background process dies you might not even notice right away because the exit message got buried somewhere in the logs.
With concurrently OTOH you can clean up background processes automatically:
{
"scripts": {
"start": "concurrently --kill-others 'json-server db.json' 'react-scripts start'"
},
"devDependencies": {
"concurrently": "^4.1.1"
}
}
Also you’ll get a more organised console output so you can see where a certain message is actually coming from.
many thanks
This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.