Welcome to the Linux Foundation Forum!

CH-7 lab

How do you set up your package.json so that you can start the three servers with one command PORT=3000 BOAT_SERVICE_PORT=3333 BRAND_SERVICE_PORT=3334 npm start

Is it necessary to use the concurrently tool ? or is there another way ?



  • davidmarkclements
    davidmarkclements Posts: 270
    edited October 2021


    the command you've described should work on Linux/Mac
    On windows you'd need to use set before running the command

    set PORT 3000
    set BOAT_SERVICE_PORT 3333
    npm start

    If you wanted to modify the package.json directly and hard code the port numbers you can inline them on Linux/Mac like so:

      "start": "PORT=3000 BOAT_SERVICE_PORT=3333 BRAND_SERVICE_PORT=3334 <whatever command you use to start the app>"

    This, obviously, won't work on Windows. To achieve something similar in Window see https://www.npmjs.com/package/cross-env - bear in mind that the exam takes place in a Linux environment.

  • nayib
    nayib Posts: 12

    Thanks @davidmarkclements

    Im using linux.

    What Im trying to do is to start the 3 servers with one command. I currently have the following in the package.json but it appears it does not work:

    "start": "PORT=3000 BOAT_SERVICE_PORT=3333 BRAND_SERVICE_PORT=3334 node brand-service.js boat-service.js && fastify star -l info app.js"

    Thanks in advance.

  • gregsheppard
    gregsheppard Posts: 31
    edited October 2021

    This format should work:

    PORT_A=4000 node serverA.js & PORT_B=5000 node serverB.js & PORT=3001 fastify start -l info app.js
  • nayib
    nayib Posts: 12

    Thanks @gregsheppard . Yes, using the & woks to run several commands in linux bash/temrinal but for some reason it is not taking the environment variables but starting the servers in other ports. I need to recheck my code implementation. Thank you.

  • I realized after I posted the above that the fastify server won't have access to the PORT_A and PORT_B ENV variables so try this instead:

    export PORT_A=4000 PORT_B=5000 PORT=3001; node serverA.js & node serverB.js & fastify start -l info app.js

  • nayib
    nayib Posts: 12

    Hi Greg. it is still don't work, but thank you so much for your help.

  • gregsheppard
    gregsheppard Posts: 31
    edited October 2021

    My apologies @nayib, I was more focused on responding with a demo of the shell grammar and hadn't tested it on the course code. I went back and fired it up and now have something verified but it got long enough to make a separate shell script that npm start executes.

    Here's the folder structure:

    ├── bicycle-service.js
    ├── brand-service.js
    └── consuming-service
        ├── app.js
        ├── node_modules
        ├── npm_start_script <-------- This is the script below
        ├── package-lock.json
        ├── package.json
        ├── plugins
        ├── routes
        └── test

    Here's the script:

    PORT="$BICYCLE_SERVICE_PORT" node ../bicycle-service.js &
    PORT="$BRAND_SERVICE_PORT" node ../brand-service.js &
    PORT=3001 fastify start -w -l info -P app.js

    And finally, this is what I have in the package.json scripts:

    "start": "sh npm_start_script"

    This way all three services have their own process.env.PORT but the consuming service can also access the ports of the other two via process.env.BICYCLE_SERVICE_PORT and process.env.BRAND_SERVICE_PORT.

    Let me know if you have any luck with this one.

  • this is as good a way as any :+1:

  • nayib
    nayib Posts: 12

    @gregsheppard thanks, it is a nice solution.

This discussion has been closed.


Upcoming Training