2

Is it possible to start both angular default server(port:4200) and node.js(port:8080) using npm?

packages.json:

"start":"ng serve && node server.js"

If I run npm start, only angular's server(4200) is started running but not node's server(8080).

Is there a way to make both the servers has to run on their respective ports at same time using npm.

Need someone's help.

Sta-Dev
  • 305
  • 5
  • 17
  • Possible duplicate of [How can I run multiple npm scripts in parallel?](https://stackoverflow.com/questions/30950032/how-can-i-run-multiple-npm-scripts-in-parallel) – baao Apr 05 '18 at 13:46
  • The second answer should work, replacing the `&&` with a pipe `|` – baao Apr 05 '18 at 13:47
  • Have you tried opening a second terminal window? – hbere Nov 03 '18 at 15:45

2 Answers2

7

Inside of your package.json you can add

"start": "ng serve --open | nodemon node/",

Keep in mind that it is relative to your package.json location , so in the above example node is a subdirectory inside my angular project. This will also open a browser for you. If you don't want the browser to open just remove the --open

Full example

"scripts": {
"ng": "ng",
"start": "ng serve --open | nodemon node/",
"build": "ng build",
"test": "ng test",
"lint": "ng lint",
"e2e": "ng e2e"

},

You can use gulp of course as the other answer suggests

Alex Mackinnon
  • 300
  • 3
  • 12
0

If you need to have control over startup scripts, maybe it is a better approach to use a tool directly designed for that. (Workflow automation)

May I recommend gulp.js: with it, you can define scripts that will run in a sequence or paralell.

A very basic example:

gulp.task('node-server-start', function (cb) {
  spawn('node', ['server.js'], {stdio: 'inherit'}) 
});
gulp.task('ng-serve', function (cb) {
  spawn('ng', ['serve'], {stdio: 'inherit'}) 
});
gulp.task('start', ['ng-serve', 'node-server-start'], function () {
});

And you just start it wih gulp start

ForestG
  • 17,538
  • 14
  • 52
  • 86