Running npm Commands on Netlify Deployment

In a previous post, I demonstrated how to set up Netlify with a static website. However, we skimmed over two fields: Publish directory and Build command.

As Build command suggests, you can run commands on every code change before deploying the site. This is important if your website requires pre-processing such as CSS minification or a bundler call. Some of the examples provided by Netlify at the time of writing are:

  • middleman build
  • grunt build
  • make all

However, what’s undocumented is that you can actually use npm commands as your build command, such as npm run build. This is likely because Netlify relies on a Node.js back-end, although there are no documentation to support this.

Try It Yourself

I have a a sample React.js application that has the following Netlify configurations, defined inside a netlify.toml file:

[build]
  base    = "."
  publish = "build"
  command = "npm run build"

This file tells Netlify to run npm run build before every deployment, and that npm run build will put the bundled static files into the build directory.

Simply fork the repo, or click on the Deploy to netlify button to get started.

Software Developer | Lifelong Learner. neverendingqs.com