Skip to content

Commit c8b73db

Browse files
wraithgarruyadorno
authored andcommitted
fix(docs): update scripts docs
The lifecycle events section was very out of date, and lots of other cleanup needed to happen too PR-URL: #2690 Credit: @wraithgar Close: #2690 Reviewed-by: @ruyadorno
1 parent 0c881fc commit c8b73db

File tree

1 file changed

+139
-52
lines changed

1 file changed

+139
-52
lines changed

docs/content/using-npm/scripts.md

+139-52
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,19 @@ description: How npm handles the "scripts" field
66

77
### Description
88

9-
The `"scripts"` property of your `package.json` file supports a number of built-in scripts and their preset life cycle events as well as arbitrary scripts. These all can be executed by running `npm run-script <stage>` or `npm run <stage>` for short. *Pre* and *post* commands with matching names will be run for those as well (e.g. `premyscript`, `myscript`, `postmyscript`). Scripts from dependencies can be run with `npm explore <pkg> -- npm run <stage>`.
9+
The `"scripts"` property of your `package.json` file supports a number
10+
of built-in scripts and their preset life cycle events as well as
11+
arbitrary scripts. These all can be executed by running `npm run-script
12+
<stage>` or `npm run <stage>` for short. *Pre* and *post* commands with
13+
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>`.
1016

1117
### Pre & Post Scripts
1218

13-
To create "pre" or "post" scripts for any scripts defined in the `"scripts"` section of the `package.json`, simply create another script *with a matching name* and add "pre" or "post" to the beginning of them.
19+
To 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.
1422

1523
```json
1624
{
@@ -22,20 +30,35 @@ To create "pre" or "post" scripts for any scripts defined in the `"scripts"` sec
2230
}
2331
```
2432

33+
In this example `npm run compress` would execute these scripts as
34+
described.
35+
2536
### Life Cycle Scripts
2637

27-
There are some special life cycle scripts that happen only in certain situations. These scripts happen in addition to the "pre" and "post" script.
38+
There are some special life cycle scripts that happen only in certain
39+
situations. These scripts happen in addition to the `pre<event>`, `post<event>`, and
40+
`<event>` scripts.
41+
2842
* `prepare`, `prepublish`, `prepublishOnly`, `prepack`, `postpack`
2943

3044
**prepare** (since `npm@4.0.0`)
45+
* Runs any time before the package is packed, i.e. during `npm publish`
46+
and `npm pack`
3147
* Runs BEFORE the package is packed
3248
* Runs BEFORE the package is published
3349
* Runs on local `npm install` without any arguments
3450
* Run AFTER `prepublish`, but BEFORE `prepublishOnly`
35-
* NOTE: If a package being installed through git contains a `prepare` script, its `dependencies` and `devDependencies` will be installed, and the prepare script will be run, before the package is packaged and installed.
51+
52+
* NOTE: If a package being installed through git contains a `prepare`
53+
script, its `dependencies` and `devDependencies` will be installed, and
54+
the prepare script will be run, before the package is packaged and
55+
installed.
56+
57+
* As of `npm@7` these scripts run in the background
3658

3759
**prepublish** (DEPRECATED)
38-
* Same as `prepare`
60+
* Does not run during `npm publish`, but does run during `npm ci`
61+
and `npm install`. See below for more info.
3962

4063
**prepublishOnly**
4164
* Runs BEFORE the package is prepared and packed, ONLY on `npm publish`.
@@ -45,7 +68,7 @@ There are some special life cycle scripts that happen only in certain situations
4568
* 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.
4669

4770
**postpack**
48-
* Runs AFTER the tarball has been generated and moved to its final destination.
71+
* 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)
4972

5073
#### Prepare and Prepublish
5174

@@ -74,51 +97,116 @@ The advantage of doing these things at `prepublish` time is that they can be don
7497

7598
### Life Cycle Operation Order
7699

77-
#### [`npm publish`](/commands/npm-publish)
100+
#### [`npm cache add`](/commands/npm-cache)
78101

79-
* `prepublishOnly`
80102
* `prepare`
103+
104+
#### [`npm ci`](/commands/npm-ci)
105+
106+
* `preinstall`
107+
* `install`
108+
* `postinstall`
81109
* `prepublish`
82-
* `publish`
83-
* `postpublish`
110+
* `preprepare`
111+
* `prepare`
112+
* `postprepare`
113+
114+
These all run after the actual installation of modules into
115+
`node_modules`, in order, with no internal actions happening in between
116+
117+
#### [`npm diff`](/commands/npm-diff)
118+
119+
* `prepare`
120+
121+
#### [`npm env`](/commands/npm-env)
122+
123+
* `env` (You can override the default behavior of `npm env` by defining
124+
a custom `env` entry in your `scripts` object)
125+
126+
#### [`npm install`](/commands/npm-install)
127+
128+
These also run when you run `npm install -g <pkg-name>`
129+
130+
* `preinstall`
131+
* `install`
132+
* `postinstall`
133+
* `prepublish`
134+
* `preprepare`
135+
* `prepare`
136+
* `postprepare`
137+
138+
If there is a `binding.gyp` file in the root of your package and you
139+
haven't defined your own `install` or `preinstall` scripts, npm will
140+
default the `install` command to compile using node-gyp via `node-gyp
141+
rebuild`
142+
143+
These are run from the scripts of `<pkg-name>`
84144

85145
#### [`npm pack`](/commands/npm-pack)
86146

87147
* `prepack`
148+
* `prepare`
88149
* `postpack`
89150

90-
#### [`npm install`](/commands/npm-install)
151+
#### [`npm publish`](/commands/npm-publish)
152+
153+
* `prepublishOnly`
154+
* `prepack`
155+
* `prepare`
156+
* `postpack`
157+
* `publish`
158+
* `postpublish`
159+
160+
`prepare` will not run during `--dry-run`
161+
162+
#### [`npm rebuild`](/commands/npm-rebuild)
91163

92164
* `preinstall`
93165
* `install`
94166
* `postinstall`
167+
* `prepare`
95168

96-
Also triggers
169+
`prepare` is only run if the current directory is a symlink (e.g. with
170+
linked packages)
97171

98-
* `prepublish` (when on local)
99-
* `prepare` (when on local or workspaces)
172+
#### [`npm restart`](/commands/npm-restart)
100173

101-
#### [`npm start`](/commands/npm-start)
174+
If 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`
102181

103-
`npm run start` has an `npm start` shorthand.
182+
#### [`npm run <user defined>`](/commands/npm-run)
183+
184+
* `pre<user-defined>`
185+
* `<user-defined>`
186+
* `post<user-defined>`
187+
188+
#### [`npm start`](/commands/npm-start)
104189

105190
* `prestart`
106191
* `start`
107192
* `poststart`
108193

109-
### Default Values
110-
npm will default some script values based on package contents.
194+
If there is a `server.js` file in the root of your package, then npm
195+
will 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)
111199

112-
* `"start": "node server.js"`:
200+
* `prestop`
201+
* `stop`
202+
* `poststop`
113203

114-
If there is a `server.js` file in the root of your package, then npm
115-
will default the `start` command to `node server.js`.
204+
#### [`npm test`](/commands/npm-test)
116205

117-
* `"install": "node-gyp rebuild"`:
206+
* `pretest`
207+
* `test`
208+
* `posttest`
118209

119-
If there is a `binding.gyp` file in the root of your package and you
120-
haven't defined your own `install` or `preinstall` scripts, npm will
121-
default the `install` command to compile using node-gyp.
122210

123211
### User
124212

@@ -131,22 +219,21 @@ Package scripts run in an environment where many pieces of information
131219
are made available regarding the setup of npm and the current state of
132220
the process.
133221

134-
135222
#### path
136223

137224
If you depend on modules that define executable scripts, like test
138225
suites, then those executables will be added to the `PATH` for
139226
executing the scripts. So, if your package.json has this:
140227

141228
```json
142-
{
143-
"name" : "foo",
144-
"dependencies" : {
145-
"bar" : "0.1.x"
146-
},
147-
"scripts": {
148-
"start" : "bar ./test"
149-
}
229+
{
230+
"name" : "foo",
231+
"dependencies" : {
232+
"bar" : "0.1.x"
233+
},
234+
"scripts": {
235+
"start" : "bar ./test"
236+
}
150237
}
151238
```
152239

@@ -159,8 +246,8 @@ The package.json fields are tacked onto the `npm_package_` prefix. So,
159246
for instance, if you had `{"name":"foo", "version":"1.2.5"}` in your
160247
package.json file, then your package scripts would have the
161248
`npm_package_name` environment variable set to "foo", and the
162-
`npm_package_version` set to "1.2.5". You can access these variables
163-
in your code with `process.env.npm_package_name` and
249+
`npm_package_version` set to "1.2.5". You can access these variables
250+
in your code with `process.env.npm_package_name` and
164251
`process.env.npm_package_version`, and so on for other fields.
165252

166253
#### configuration
@@ -176,14 +263,14 @@ there is a config param of `<name>[@<version>]:<key>`. For example,
176263
if the package.json has this:
177264

178265
```json
179-
{
180-
"name" : "foo",
181-
"config" : {
182-
"port" : "8080"
183-
},
184-
"scripts" : {
185-
"start" : "node server.js"
186-
}
266+
{
267+
"name" : "foo",
268+
"config" : {
269+
"port" : "8080"
270+
},
271+
"scripts" : {
272+
"start" : "node server.js"
273+
}
187274
}
188275
```
189276

@@ -219,10 +306,10 @@ process.env.npm_package_scripts_install === "foo.js"
219306
For example, if your package.json contains this:
220307

221308
```json
222-
{
223-
"scripts" : {
224-
"install" : "scripts/install.js",
225-
"postinstall" : "scripts/postinstall.js",
309+
{
310+
"scripts" : {
311+
"install" : "scripts/install.js",
312+
"postinstall" : "scripts/postinstall.js",
226313
"uninstall" : "scripts/uninstall.js"
227314
}
228315
}
@@ -239,10 +326,10 @@ If you want to run a make command, you can do so. This works just
239326
fine:
240327

241328
```json
242-
{
243-
"scripts" : {
244-
"preinstall" : "./configure",
245-
"install" : "make && make install",
329+
{
330+
"scripts" : {
331+
"preinstall" : "./configure",
332+
"install" : "make && make install",
246333
"test" : "make test"
247334
}
248335
}

0 commit comments

Comments
 (0)