Node.js package.json Scripts


Example

You can define scripts that can be executed or are triggered before or after another script.

{
  "scripts": {
    "pretest": "scripts/pretest.js",
    "test": "scripts/test.js",
    "posttest": "scripts/posttest.js"
  }
}

In this case, you can execute the script by running either of these commands:

$ npm run-script test
$ npm run test
$ npm test
$ npm t

Pre-defined scripts

Script NameDescription
prepublishRun before the package is published.
publish, postpublishRun after the package is published.
preinstallRun before the package is installed.
install, postinstallRun after the package is installed.
preuninstall, uninstallRun before the package is uninstalled.
postuninstallRun after the package is uninstalled.
preversion, versionRun before bump the package version.
postversionRun after bump the package version.
pretest, test, posttestRun by the npm test command
prestop, stop, poststopRun by the npm stop command
prestart, start, poststartRun by the npm start command
prerestart, restart, postrestartRun by the npm restart command

User-defined scripts

You can also define your own scripts the same way you do with the pre-defined scripts:

{
  "scripts": {
    "preci": "scripts/preci.js",
    "ci": "scripts/ci.js",
    "postci": "scripts/postci.js"
  }
}

In this case, you can execute the script by running either of these commands:

$ npm run-script ci
$ npm run ci

User-defined scripts also supports pre and post scripts, as shown in the example above.