1--- 2title: scripts 3section: 7 4description: How npm handles the "scripts" field 5--- 6 7### Description 8 9The `"scripts"` property of your `package.json` file supports a number 10of built-in scripts and their preset life cycle events as well as 11arbitrary scripts. These all can be executed by running 12`npm run-script <stage>` or `npm run <stage>` for short. *Pre* and *post* 13commands with matching names will be run for those as well (e.g. `premyscript`, 14`myscript`, `postmyscript`). Scripts from dependencies can be run with 15`npm explore <pkg> -- npm run <stage>`. 16 17### Pre & Post Scripts 18 19To create "pre" or "post" scripts for any scripts defined in the 20`"scripts"` section of the `package.json`, simply create another script 21*with a matching name* and add "pre" or "post" to the beginning of them. 22 23```json 24{ 25 "scripts": { 26 "precompress": "{{ executes BEFORE the `compress` script }}", 27 "compress": "{{ run command to compress files }}", 28 "postcompress": "{{ executes AFTER `compress` script }}" 29 } 30} 31``` 32 33In this example `npm run compress` would execute these scripts as 34described. 35 36### Life Cycle Scripts 37 38There are some special life cycle scripts that happen only in certain 39situations. These scripts happen in addition to the `pre<event>`, `post<event>`, and 40`<event>` scripts. 41 42* `prepare`, `prepublish`, `prepublishOnly`, `prepack`, `postpack`, `dependencies` 43 44**prepare** (since `npm@4.0.0`) 45* Runs BEFORE the package is packed, i.e. during `npm publish` 46 and `npm pack` 47* Runs on local `npm install` without any arguments 48* Runs AFTER `prepublish`, but BEFORE `prepublishOnly` 49 50* NOTE: If a package being installed through git contains a `prepare` 51 script, its `dependencies` and `devDependencies` will be installed, and 52 the prepare script will be run, before the package is packaged and 53 installed. 54 55* As of `npm@7` these scripts run in the background. 56 To see the output, run with: `--foreground-scripts`. 57 58**prepublish** (DEPRECATED) 59* Does not run during `npm publish`, but does run during `npm ci` 60 and `npm install`. See below for more info. 61 62**prepublishOnly** 63* Runs BEFORE the package is prepared and packed, ONLY on `npm publish`. 64 65**prepack** 66* Runs BEFORE a tarball is packed (on "`npm pack`", "`npm publish`", and when installing a git dependency). 67* NOTE: "`npm run pack`" is NOT the same as "`npm pack`". "`npm run pack`" is an arbitrary user defined script name, where as, "`npm pack`" is a CLI defined command. 68 69**postpack** 70* Runs AFTER the tarball has been generated but before it is moved to its final destination (if at all, publish does not save the tarball locally) 71 72**dependencies** 73* Runs AFTER any operations that modify the `node_modules` directory IF changes occurred. 74* Does NOT run in global mode 75 76#### Prepare and Prepublish 77 78**Deprecation Note: prepublish** 79 80Since `npm@1.1.71`, the npm CLI has run the `prepublish` script for both `npm publish` and `npm install`, because it's a convenient way to prepare a package for use (some common use cases are described in the section below). It has also turned out to be, in practice, [very confusing](https://github.com/npm/npm/issues/10074). As of `npm@4.0.0`, a new event has been introduced, `prepare`, that preserves this existing behavior. A _new_ event, `prepublishOnly` has been added as a transitional strategy to allow users to avoid the confusing behavior of existing npm versions and only run on `npm publish` (for instance, running the tests one last time to ensure they're in good shape). 81 82See <https://github.com/npm/npm/issues/10074> for a much lengthier justification, with further reading, for this change. 83 84**Use Cases** 85 86If you need to perform operations on your package before it is used, in a way that is not dependent on the operating system or architecture of the target system, use a `prepublish` script. This includes tasks such as: 87 88* Compiling CoffeeScript source code into JavaScript. 89* Creating minified versions of JavaScript source code. 90* Fetching remote resources that your package will use. 91 92The advantage of doing these things at `prepublish` time is that they can be done once, in a single place, thus reducing complexity and variability. Additionally, this means that: 93 94* You can depend on `coffee-script` as a `devDependency`, and thus 95 your users don't need to have it installed. 96* You don't need to include minifiers in your package, reducing 97 the size for your users. 98* You don't need to rely on your users having `curl` or `wget` or 99 other system tools on the target machines. 100 101#### Dependencies 102 103The `dependencies` script is run any time an `npm` command causes changes to the `node_modules` directory. It is run AFTER the changes have been applied and the `package.json` and `package-lock.json` files have been updated. 104 105### Life Cycle Operation Order 106 107#### [`npm cache add`](/commands/npm-cache) 108 109* `prepare` 110 111#### [`npm ci`](/commands/npm-ci) 112 113* `preinstall` 114* `install` 115* `postinstall` 116* `prepublish` 117* `preprepare` 118* `prepare` 119* `postprepare` 120 121 These all run after the actual installation of modules into 122 `node_modules`, in order, with no internal actions happening in between 123 124#### [`npm diff`](/commands/npm-diff) 125 126* `prepare` 127 128#### [`npm install`](/commands/npm-install) 129 130These also run when you run `npm install -g <pkg-name>` 131 132* `preinstall` 133* `install` 134* `postinstall` 135* `prepublish` 136* `preprepare` 137* `prepare` 138* `postprepare` 139 140If there is a `binding.gyp` file in the root of your package and you 141haven't defined your own `install` or `preinstall` scripts, npm will 142default the `install` command to compile using node-gyp via `node-gyp 143rebuild` 144 145These are run from the scripts of `<pkg-name>` 146 147#### [`npm pack`](/commands/npm-pack) 148 149* `prepack` 150* `prepare` 151* `postpack` 152 153#### [`npm publish`](/commands/npm-publish) 154 155* `prepublishOnly` 156* `prepack` 157* `prepare` 158* `postpack` 159* `publish` 160* `postpublish` 161 162#### [`npm rebuild`](/commands/npm-rebuild) 163 164* `preinstall` 165* `install` 166* `postinstall` 167* `prepare` 168 169`prepare` is only run if the current directory is a symlink (e.g. with 170linked packages) 171 172#### [`npm restart`](/commands/npm-restart) 173 174If there is a `restart` script defined, these events are run, otherwise 175`stop` and `start` are both run if present, including their `pre` and 176`post` iterations) 177 178* `prerestart` 179* `restart` 180* `postrestart` 181 182#### [`npm run <user defined>`](/commands/npm-run-script) 183 184* `pre<user-defined>` 185* `<user-defined>` 186* `post<user-defined>` 187 188#### [`npm start`](/commands/npm-start) 189 190* `prestart` 191* `start` 192* `poststart` 193 194If there is a `server.js` file in the root of your package, then npm 195will default the `start` command to `node server.js`. `prestart` and 196`poststart` will still run in this case. 197 198#### [`npm stop`](/commands/npm-stop) 199 200* `prestop` 201* `stop` 202* `poststop` 203 204#### [`npm test`](/commands/npm-test) 205 206* `pretest` 207* `test` 208* `posttest` 209 210#### [`npm version`](/commands/npm-version) 211 212* `preversion` 213* `version` 214* `postversion` 215 216#### A Note on a lack of [`npm uninstall`](/commands/npm-uninstall) scripts 217 218While npm v6 had `uninstall` lifecycle scripts, npm v7 does not. Removal of a package can happen for a wide variety of reasons, and there's no clear way to currently give the script enough context to be useful. 219 220Reasons for a package removal include: 221 222* a user directly uninstalled this package 223* a user uninstalled a dependant package and so this dependency is being uninstalled 224* a user uninstalled a dependant package but another package also depends on this version 225* this version has been merged as a duplicate with another version 226* etc. 227 228Due to the lack of necessary context, `uninstall` lifecycle scripts are not implemented and will not function. 229 230### User 231 232When npm is run as root, scripts are always run with the effective uid 233and gid of the working directory owner. 234 235### Environment 236 237Package scripts run in an environment where many pieces of information 238are made available regarding the setup of npm and the current state of 239the process. 240 241#### path 242 243If you depend on modules that define executable scripts, like test 244suites, then those executables will be added to the `PATH` for 245executing the scripts. So, if your package.json has this: 246 247```json 248{ 249 "name" : "foo", 250 "dependencies" : { 251 "bar" : "0.1.x" 252 }, 253 "scripts": { 254 "start" : "bar ./test" 255 } 256} 257``` 258 259then you could run `npm start` to execute the `bar` script, which is 260exported into the `node_modules/.bin` directory on `npm install`. 261 262#### package.json vars 263 264The package.json fields are tacked onto the `npm_package_` prefix. So, 265for instance, if you had `{"name":"foo", "version":"1.2.5"}` in your 266package.json file, then your package scripts would have the 267`npm_package_name` environment variable set to "foo", and the 268`npm_package_version` set to "1.2.5". You can access these variables 269in your code with `process.env.npm_package_name` and 270`process.env.npm_package_version`, and so on for other fields. 271 272See [`package.json`](/configuring-npm/package-json) for more on package configs. 273 274#### current lifecycle event 275 276Lastly, the `npm_lifecycle_event` environment variable is set to 277whichever stage of the cycle is being executed. So, you could have a 278single script used for different parts of the process which switches 279based on what's currently happening. 280 281Objects are flattened following this format, so if you had 282`{"scripts":{"install":"foo.js"}}` in your package.json, then you'd 283see this in the script: 284 285```bash 286process.env.npm_package_scripts_install === "foo.js" 287``` 288 289### Examples 290 291For example, if your package.json contains this: 292 293```json 294{ 295 "scripts" : { 296 "install" : "scripts/install.js", 297 "postinstall" : "scripts/install.js" 298 } 299} 300``` 301 302then `scripts/install.js` will be called for the install and post-install 303stages of the lifecycle. Since `scripts/install.js` is running for two 304different phases, it would be wise in this case to look at the 305`npm_lifecycle_event` environment variable. 306 307If you want to run a make command, you can do so. This works just 308fine: 309 310```json 311{ 312 "scripts" : { 313 "preinstall" : "./configure", 314 "install" : "make && make install", 315 "test" : "make test" 316 } 317} 318``` 319 320### Exiting 321 322Scripts are run by passing the line as a script argument to `sh`. 323 324If the script exits with a code other than 0, then this will abort the 325process. 326 327Note that these script files don't have to be Node.js or even 328JavaScript programs. They just have to be some kind of executable 329file. 330 331### Best Practices 332 333* Don't exit with a non-zero error code unless you *really* mean it. 334 If the failure is minor or only will prevent some optional features, then 335 it's better to just print a warning and exit successfully. 336* Try not to use scripts to do what npm can do for you. Read through 337 [`package.json`](/configuring-npm/package-json) to see all the things that you can specify and enable 338 by simply describing your package appropriately. In general, this 339 will lead to a more robust and consistent state. 340* Inspect the env to determine where to put things. For instance, if 341 the `npm_config_binroot` environment variable is set to `/home/user/bin`, then 342 don't try to install executables into `/usr/local/bin`. The user 343 probably set it up that way for a reason. 344* Don't prefix your script commands with "sudo". If root permissions 345 are required for some reason, then it'll fail with that error, and 346 the user will sudo the npm command in question. 347* Don't use `install`. Use a `.gyp` file for compilation, and `prepare` 348 for anything else. You should almost never have to explicitly set a 349 preinstall or install script. If you are doing this, please consider if 350 there is another option. The only valid use of `install` or `preinstall` 351 scripts is for compilation which must be done on the target architecture. 352* Scripts are run from the root of the package folder, regardless of what the 353 current working directory is when `npm` is invoked. If you want your 354 script to use different behavior based on what subdirectory you're in, you 355 can use the `INIT_CWD` environment variable, which holds the full path you 356 were in when you ran `npm run`. 357 358### See Also 359 360* [npm run-script](/commands/npm-run-script) 361* [package.json](/configuring-npm/package-json) 362* [npm developers](/using-npm/developers) 363* [npm install](/commands/npm-install) 364