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

4
  • 2
    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 and using its run-s and run-p ays of running multiple tasks Commented Dec 16, 2021 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? Commented Dec 16, 2021 at 16:24
  • @Andy yes, my app should be deployed once with a single command. Commented Dec 16, 2021 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. Commented Dec 16, 2021 at 16:46

3 Answers 3

2

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"
  }
Sign up to request clarification or add additional context in comments.

Comments

1

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.

1 Comment

I am actually running it on server-less platform. Maybe you can take a look on that separated thread? stackoverflow.com/questions/70410653/…
1

Any script in package.json file has pre and post "Life Cycle" scripts available at any time. For example - if you want to execute an extra command before executing start script - create a new command with a prefixed same name like prestart. After that when you run npm run start it will first run prestart then it will run start and after that, it will run poststart in case of any.

To test it run the following commands - more about them here

npm pkg set scripts.prestart='echo "pre start"'
npm pkg set scripts.start='echo "start"'
npm pkg set scripts.poststart='echo "post start"'

then by running npm run start your output will be something like

pre start
...
start
...
post start

More info about Life Cycle Scripts here

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.