You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The lifecycle events section was very out of date, and lots
of other cleanup needed to happen too
PR-URL: #2690
Credit: @wraithgarClose: #2690
Reviewed-by: @ruyadorno
Copy file name to clipboardexpand all lines: docs/content/using-npm/scripts.md
+139-52
Original file line number
Diff line number
Diff line change
@@ -6,11 +6,19 @@ description: How npm handles the "scripts" field
6
6
7
7
### Description
8
8
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>`.
10
16
11
17
### Pre & Post Scripts
12
18
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.
14
22
15
23
```json
16
24
{
@@ -22,20 +30,35 @@ To create "pre" or "post" scripts for any scripts defined in the `"scripts"` sec
22
30
}
23
31
```
24
32
33
+
In this example `npm run compress` would execute these scripts as
34
+
described.
35
+
25
36
### Life Cycle Scripts
26
37
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
* Runs any time before the package is packed, i.e. during `npm publish`
46
+
and `npm pack`
31
47
* Runs BEFORE the package is packed
32
48
* Runs BEFORE the package is published
33
49
* Runs on local `npm install` without any arguments
34
50
* 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
36
58
37
59
**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.
39
62
40
63
**prepublishOnly**
41
64
* 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
45
68
* 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.
46
69
47
70
**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)
49
72
50
73
#### Prepare and Prepublish
51
74
@@ -74,51 +97,116 @@ The advantage of doing these things at `prepublish` time is that they can be don
74
97
75
98
### Life Cycle Operation Order
76
99
77
-
#### [`npm publish`](/commands/npm-publish)
100
+
#### [`npm cache add`](/commands/npm-cache)
78
101
79
-
*`prepublishOnly`
80
102
*`prepare`
103
+
104
+
#### [`npm ci`](/commands/npm-ci)
105
+
106
+
*`preinstall`
107
+
*`install`
108
+
*`postinstall`
81
109
*`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>`
84
144
85
145
#### [`npm pack`](/commands/npm-pack)
86
146
87
147
*`prepack`
148
+
*`prepare`
88
149
*`postpack`
89
150
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)
91
163
92
164
*`preinstall`
93
165
*`install`
94
166
*`postinstall`
167
+
*`prepare`
95
168
96
-
Also triggers
169
+
`prepare` is only run if the current directory is a symlink (e.g. with
170
+
linked packages)
97
171
98
-
*`prepublish` (when on local)
99
-
*`prepare` (when on local or workspaces)
172
+
#### [`npm restart`](/commands/npm-restart)
100
173
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`
102
181
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)
104
189
105
190
*`prestart`
106
191
*`start`
107
192
*`poststart`
108
193
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)
111
199
112
-
*`"start": "node server.js"`:
200
+
*`prestop`
201
+
*`stop`
202
+
*`poststop`
113
203
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)
116
205
117
-
*`"install": "node-gyp rebuild"`:
206
+
*`pretest`
207
+
*`test`
208
+
*`posttest`
118
209
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.
122
210
123
211
### User
124
212
@@ -131,22 +219,21 @@ Package scripts run in an environment where many pieces of information
131
219
are made available regarding the setup of npm and the current state of
132
220
the process.
133
221
134
-
135
222
#### path
136
223
137
224
If you depend on modules that define executable scripts, like test
138
225
suites, then those executables will be added to the `PATH` for
139
226
executing the scripts. So, if your package.json has this:
140
227
141
228
```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
+
}
150
237
}
151
238
```
152
239
@@ -159,8 +246,8 @@ The package.json fields are tacked onto the `npm_package_` prefix. So,
159
246
for instance, if you had `{"name":"foo", "version":"1.2.5"}` in your
160
247
package.json file, then your package scripts would have the
161
248
`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
164
251
`process.env.npm_package_version`, and so on for other fields.
165
252
166
253
#### configuration
@@ -176,14 +263,14 @@ there is a config param of `<name>[@<version>]:<key>`. For example,
0 commit comments