2

I have a requirement to run both my angular application and JSON-server simultaneously when I run "ng serve" or "npm run start", rather than having to start each one separately.

How do I modify the startup scripts to do so? Note: This is running in Windows.

Edit: I've changed the start script in my package.json to the following, but it only starts up json-server, not the Angular application:

"start": "json-server --watch db.json && ng-serve",

Solution: I ended up solving this by installing ```npm-run-all``, and adding new entries to the scripts in package.json:

  1. "json-server" : "json-server --watch db.json"
  2. "serve" : "ng serve"
  3. "start" : "run-p json-server serve"

I never figured out why "&&" didn't work.

Jason
  • 3,943
  • 12
  • 64
  • 104
  • 1
    try here: [How to concurrently run nodejs and angular4 with single command?](https://stackoverflow.com/questions/47608173/how-to-concurrently-run-nodejs-and-angular4-with-single-command) – traynor Jan 12 '23 at 13:23

3 Answers3

1

Did you try using:

"start": "json-server --watch db.json & ng-serve",

with a single & ?

if command1 succeeds then execute command2 (IF) -> command1 && command2

Execute command1 and then execute command2 (AND) -> command1 & command2

Execute command2 only if command1 fails (OR) -> command1 || command2

Your json-server maybe is not "returning" a "success" code

Thartalion
  • 11
  • 1
  • Yes, I've tried both & and &&. Basically, it runs the first command but not the second. Your idea of not returning a success code makes a lot of sense, but I still need both to run with a single command. – Jason Jan 12 '23 at 13:06
1

It Worked for me like this,

in package.json

"start": "json-server --watch db.json & ng serve"

then i used,

npm run start

And it worked for me like this.

0

I think it's simple to explain.

&& = sequential execution

& = parallel execution

And the reason why your 'ng serve' was not executed is by the fact, that it only executes when the first script has completed.

In your case you're starting a json-server, but the server is running and watching the db.json file, which means this program never ends. So the ng serve will not be executed.

Catweazle
  • 105
  • 1
  • 8