Run an npm script in package.json inside another npm script in package.json
Leonel Elimpe
by Leonel Elimpe
~1 min read

Tags

  • Javascript
  • NPM

This post might seem trivial but I did spend a good amount of time searching this online just to be sure. So here we are lol.

Say you have an npm script called "build" inside package.json to build your application, and another script called "deploy:staging" to deploy your application to your staging environment. You’d like the deploy script to run the build script before deploying, how do you do that? Well, just call the build script inside the deploy script as you’d do from the terminal: npm run build. See below.

{
  // ...
  
  "scripts": {
    // ...
    
    "build": "ng build",
    "deploy:staging": "npm run build && netlify deploy",
    
    // ...
  },

Notice npm run build being called inside the deploy:staging script before a deploy is triggered with the Netlify CLI.


Further reading

Helpers and Tips for NPM Run Scripts - Michael Kuehnel