0

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..

jrz
  • 1,213
  • 4
  • 20
  • 54
  • 1
    it's the same syntax as standard shell scripts, so `npm install && DEBUG=my-app node src/index.js` or if you have lots of scripts that you want to run in serial/parallel combinations, it's generally easier to add in [npm-run-all](https://www.npmjs.com/package/npm-run-all) and using its `run-s` and `run-p` ays of running multiple tasks – Mike 'Pomax' Kamermans Dec 16 '21 at 16:21
  • 2
    That would mean every time you start your app all your modules would installed all over again. Are you really sure that's what you want? – Andy Dec 16 '21 at 16:24
  • @Andy yes, my app should be deployed once with a single command. – jrz Dec 16 '21 at 16:32
  • @jrz the problem with this would be, that the startup of your app will be quite slow. Additionally if npm has a downtime, you potentially won't be able to start your service. – theshark Dec 16 '21 at 16:46

2 Answers2

1

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"
  }
JimmyWj4EHiM
  • 393
  • 4
  • 12
0

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.

Bergur
  • 3,962
  • 12
  • 20
  • I am actually running it on server-less platform. Maybe you can take a look on that separated thread? https://stackoverflow.com/questions/70410653/running-node-js-on-cloudflare-pages – jrz Dec 19 '21 at 11:03