Home > Back-end >  How do we specify within a server environment which node.js start script to run?
How do we specify within a server environment which node.js start script to run?

Time:12-12

This may be a dumb question, but I'm stumped.

I have 3 environments: Dev(local), Staging(remote) and Production(remote).

I'm writing a relatively simply Express App.

In my package.json file, I have specified start scripts for each of the 3 environments. Here's the full file:

{
  "name": "test-app",
  "version": "0.0.1",
  "description": "test utility app",
  "private": true,
  "scripts": {
    "start": "cross-env NODE_ENV=development nodemon ./bin/www",
    "start:staging": "cross-env NODE_ENV=staging nodemon ./bin/www",
    "start:production": "cross-env NODE_ENV=production && nodemon ./bin/www"
  },
  "devDependencies": {
    "cross-env": "^7.0.3"
  },
  "dependencies": {
    "axios": "^0.24.0",
    "config": "^3.3.6",
    "cookie-parser": "~1.4.4",
    "cors": "^2.8.5",
    "debug": "~2.6.9",
    "dotenv": "^8.2.0",
    "express": "^4.17.1",
    "http-errors": "~1.6.3",
    "jade": "~1.11.0",
    "morgan": "~1.9.1",
    "nodemon": "^2.0.15"
  }
}

Now, my question is, how do I specify which start script should be used in each remote server environment?

I understand how to start the development environment, so ignore that and focus on Staging and Production.

For example, I want the start:staging to be executed in Staging and the start:production to be executed in Production.

I may be thinking about this incorrectly, so please let me know if I should be approaching the Staging and Production environments differently.

It just seems to me the Staging and Production environments would have no idea which environment they should operate as without me assigning them as one or the other somehow.

Maybe this is done as part of the workflow? I'm using github actions for build/deployment, fyi.

Update

So, I did a little more digging. In the workflow file, there's a build step:

 name: npm install, build, and test
        run: |
          npm install
          npm run build --if-present
          npm run test --if-present

Would I simply add something like: npm run start:staging or npm run start:production as appropriate?

CodePudding user response:

You basically want to run the scripts and don't know the commands to run, right?

So

npm run start:staging

On gh-actions

- name: run app on staging
run: npm run start:staging
  • Related