1

I'm trying to start a json-server and start my React-App in the same script in package.json ...

But the problem is that none of the two scripts give me the prompt back so the second script is never launched ...

I've tried :

"scripts": {
   "start": "npm run start-db & npm start-react",
   "start-react": "react-scripts start && exit 1",
   "start-db": "json-server --watch src/bdd.json -p 3001 && exit 1"
}

with differents combinations of && or &... With or without exit 1 ( things that I found here )

Seba99
  • 1,197
  • 14
  • 38
  • Open multiple terminals and run each in a separate terminal – illiteratewriter Sep 04 '18 at 10:08
  • Well ... That's what I'm doing for the moment ... Isn't it possible to merge this in package.json ? – Seba99 Sep 04 '18 at 10:08
  • I don't think so, since both of them are seperate servers. Both are on seperate port numbers right? – illiteratewriter Sep 04 '18 at 10:13
  • Yes ``react-scripts start`` serve on default port ( 3000 ) that's why I've specified ``-p 3001`` for the json-server command – Seba99 Sep 04 '18 at 10:15
  • these are two servers. you cannot run multiple instances of a server on one shell. This is because the server has to keep on running and provide data whenever queried upon. When you exit, there is no server to listen to the queries. – illiteratewriter Sep 04 '18 at 11:33

2 Answers2

1

you can pass multiple commands like this

npm run start-db start-react
Amruth
  • 5,792
  • 2
  • 28
  • 41
  • Still get stuck in the ``json-server --watch src/bdd.json -p 3001`` command and don't start react app – Seba99 Sep 04 '18 at 10:06
  • I'm not getting any error ... The script is just waitting to log user's interactions with the server like ``GET /people 304 15.296 ms``... So it doesn't give back hand ... So the second script isn't launched – Seba99 Sep 04 '18 at 10:13
0

you can use a tool like concurrently.

"scripts": {
    "start": "concurrently \"npm:start-db\" \"npm:start-react\"",
    "start-react": "react-scripts start",
    "start-db": "json-server --watch src/bdd.json -p 3001"
}

after installing it with npm i --save-dev concurrently.

Johannes Buchholz
  • 1,857
  • 19
  • 34