Home > Net >  node packge.json scripts - add command to existing script
node packge.json scripts - add command to existing script

Time:12-17

I'd like to add a command to the "start" script so when I do npm start, first thing that will run is npm install.

My package.json looks as follows:

.
.
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "DEBUG=my-app node src/index.js",
    "dev": "nodemon src/index.js"
  },
.
.
.

I thought about adding npm install inside the start script:

   "start": "npm install DEBUG=my-app node src/index.js",

But this doesn't work so I'd like to get a suggestion, wether if it's even possible..

CodePudding user response:

I think you only use the && conector. like:

  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "npm install && DEBUG=my-app node src/index.js",
    "dev": "nodemon src/index.js"
  }

CodePudding user response:

Andy yes, my app should be deployed once with a single command.

This is a quite a heavy/slow start, installing all modules before starting the app.

That means if I change the code somewhwere in my node server, stop the process and run it again, a full install is gonna happen. I realize that you have a dev script with nodemon, but still.

Another case: If your app crashes on the live server and you need to start it up again then a full install will happen. What happens if a module has gone up a patch or a minor version. That means you will start up a project with different dependencies.

If you're doing this in the ci/cd then a pipeline is usually split up:

  1. Install - npm ci
  2. Build/compile - for example if you have typescript (not in your case)
  3. Run all tests
  4. Remove the devDependencies with npm prune
  5. Start up the process

What you would maybe do is have a script called "pipeline" or something, and then call that.

"pipeline": npm ci && npm run build && npm test && npm prune && npm start

This script would then be called in your pipeline code.

  • Related