Idea: Dependencies for NPM run scripts

(Eric Dahlseng) #1

Often times a specific NPM run script has dependencies. Take npm start, for example. I usually have source code to transpile before starting the server. So ideally npm run build needs to be run first. Unfortunately npm run build requires packages that need to be installed, so npm install needs to be run before that. I’d love people to be able to just get started in my repository, and in a perfect world they could just run npm start

Key Insight
Makefiles already support this behavior! Each target in a Makefile has a list of prerequisites. If a prerequisite is a file, then make checks to see if the file has been changed, running any intermediary recipes needed to properly satisfy the target.

NPM run scripts could also contain dependency information, allowing a similar behavior with npm run. Here’s a sketch of how the package.json file could look:

  "scripts": {
    "build": {
      "command": "babel sources --out-dir dist",
      "depends": ["install", "./sources"],
      "produces": ["./dist"]
    "start": {
      "command": "node ./dist/server.js",
      "depends": ["build"],

If a script is just a string, then it operates as a normal command alias. If the script is an object, however, then it is possible to specify dependency information. In the example above, running npm start would first run npm install, then would run npm run build, and finally would run npm start. Since ./sources is listed as a dependency for npm run build, then if any of the files were edited in this directory, running npm start would cause npm run build to be re-triggered.