5

My NPM package has the following scripts to

  1. Start MongoDB.
  2. Build an angular app and having it watched with any changes.
  3. Start an express server using nodemon.

    "scripts": {
    "start_db": "mongod",
    "start_client": "ng build --output-path ./server/public/ --watch",
    "start_server": "nodemon --inspect -w server ./server/server.js",
    "start_all": "concurrently \"mongod\" \"nodemon --inspect -w server ./server/server.js\" \"ng build --output-path ./server/public/ --watch\""}
    

I want to do them all using a single command, so I add a script "start_all" and use concurrently.

However, when the express server starts, mongodb is not yet started and errors report.

[1] 2019-07-15 13:36:54 - error - Connection to DB failed. MongoNetworkError: failed to connect to server [localhost:27017] on first connect [MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017] [1] at Pool. (/repository/dashboard-js/node_modules/mongodb-core/lib/topologies/server.js:431:11) [1] at Pool.emit (events.js:198:13) [1] at connect (/repository/dashboard-js/node_modules/mongodb-core/lib/connection/pool.js:557:14) [1] at makeConnection (/repository/dashboard-js/node_modules/mongodb-core/lib/connection/connect.js:39:11) [1] at callback (/repository/dashboard-js/node_modules/mongodb-core/lib/connection/connect.js:261:5) [1] at Socket.err (/repository/dashboard-js/node_modules/mongodb-core/lib/connection/connect.js:286:7) [1] at Object.onceWrapper (events.js:286:20) [1] at Socket.emit (events.js:198:13) [1] at emitErrorNT (internal/streams/destroy.js:91:8) [1] at emitErrorAndCloseNT (internal/streams/destroy.js:59:3) [1] at process._tickCallback (internal/process/next_tick.js:63:19) [1] [nodemon] app crashed - waiting for file changes before starting...

Are there any way to fix this issue? For example, can I configure concurrently so that it only executes "start_client" and "start_server" after "start_db" completes?

Kelvin Ho
  • 376
  • 2
  • 3
  • 14
  • (1) Why are using "concurrently" if you need to start things sequentially? (2) Even if you start sequentially, there is a delay between mongo reporting a successful start and it being ready to accept requests. I'd look at something like `wait_for` to wait for mongo be actually available before doing the next action. – 9000 Jul 15 '19 at 03:39
  • I once asked and answered a question very similar to this https://stackoverflow.com/a/53605477/1563833. Basically making use of `&&` to run scripts sequentially might be the only hint you need from that answer. – Wyck Jul 15 '19 at 03:53

0 Answers0