codekingpro/portable-devtools
115k
1.TH "SCRIPTS" "7" "April 2026" "NPM@11.13.0" ""
2.SH "NAME"
3\fBScripts\fR - How npm handles the "scripts" field
4.SS "Description"
5.P
6The \fB"scripts"\fR property of your \fBpackage.json\fR 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 \fBnpm run <stage>\fR. \fIPre\fR and \fIpost\fR commands with matching names will be run for those as well (e.g. \fBpremyscript\fR, \fBmyscript\fR, \fBpostmyscript\fR). Scripts from dependencies can be run with \fBnpm explore <pkg> -- npm run <stage>\fR.
7.SS "Pre & Post Scripts"
8.P
9To create "pre" or "post" scripts for any scripts defined in the \fB"scripts"\fR section of the \fBpackage.json\fR, simply create another script \fIwith a matching name\fR and add "pre" or "post" to the beginning of them.
10.P
11.RS 2
12.nf
13{
14 "scripts": {
15 "precompress": "{{ executes BEFORE the `compress` script }}",
16 "compress": "{{ run command to compress files }}",
17 "postcompress": "{{ executes AFTER `compress` script }}"
18 }
19}
20.fi
21.RE
22.P
23In this example \fBnpm run compress\fR would execute these scripts as described.
24.SS "Life Cycle Scripts"
25.P
26There are some special life cycle scripts that happen only in certain situations. These scripts happen in addition to the \fBpre<event>\fR, \fBpost<event>\fR, and \fB<event>\fR scripts.
27.RS 0
28.IP \(bu 4
29\fBprepare\fR, \fBprepublish\fR, \fBprepublishOnly\fR, \fBprepack\fR, \fBpostpack\fR, \fBdependencies\fR
30.RE 0
31
32.P
33\fBprepare\fR (since \fBnpm@4.0.0\fR)
34.RS 0
35.IP \(bu 4
36Runs BEFORE the package is packed, i.e. during \fBnpm publish\fR and \fBnpm pack\fR
37.IP \(bu 4
38Runs on local \fBnpm install\fR without package arguments (runs with flags like \fB--production\fR or \fB--omit=dev\fR, but does not run when installing specific packages like \fBnpm install express\fR)
39.IP \(bu 4
40Runs AFTER \fBprepublishOnly\fR and \fBprepack\fR, but BEFORE \fBpostpack\fR
41.IP \(bu 4
42Runs for a package if it's being installed as a link through \fBnpm install <folder>\fR
43.IP \(bu 4
44NOTE: If a package being installed through git contains a \fBprepare\fR script, its \fBdependencies\fR and \fBdevDependencies\fR will be installed, and the prepare script will be run, before the package is packaged and installed.
45.IP \(bu 4
46As of \fBnpm@7\fR these scripts run in the background. To see the output, run with: \fB--foreground-scripts\fR.
47.IP \(bu 4
48\fBIn workspaces, prepare scripts run concurrently\fR across all packages. If you have interdependent packages where one must build before another, consider using \fB--foreground-scripts\fR (which can be set in \fB.npmrc\fR with \fBforeground-scripts=true\fR) to run scripts sequentially, or structure your build differently.
49.RE 0
50
51.P
52\fBprepublish\fR (DEPRECATED)
53.RS 0
54.IP \(bu 4
55Does not run during \fBnpm publish\fR, but does run during \fBnpm ci\fR and \fBnpm install\fR. See below for more info.
56.RE 0
57
58.P
59\fBprepublishOnly\fR
60.RS 0
61.IP \(bu 4
62Runs BEFORE the package is prepared and packed, ONLY on \fBnpm publish\fR.
63.RE 0
64
65.P
66\fBprepack\fR
67.RS 0
68.IP \(bu 4
69Runs BEFORE a tarball is packed (on "\fBnpm pack\fR", "\fBnpm publish\fR", and when installing a git dependency).
70.IP \(bu 4
71NOTE: "\fBnpm run pack\fR" is NOT the same as "\fBnpm pack\fR". "\fBnpm run pack\fR" is an arbitrary user defined script name, whereas, "\fBnpm pack\fR" is a CLI defined command.
72.RE 0
73
74.P
75\fBpostpack\fR
76.RS 0
77.IP \(bu 4
78Runs 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)
79.RE 0
80
81.P
82\fBdependencies\fR
83.RS 0
84.IP \(bu 4
85Runs AFTER any operations that modify the \fBnode_modules\fR directory IF changes occurred.
86.IP \(bu 4
87Does NOT run in global mode
88.RE 0
89
90.SS "Prepare and Prepublish"
91.P
92\fBDeprecation Note: prepublish\fR
93.P
94Since \fBnpm@1.1.71\fR, the npm CLI has run the \fBprepublish\fR script for both \fBnpm publish\fR and \fBnpm install\fR, 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, \fBvery confusing\fR \fI\(lahttps://github.com/npm/npm/issues/10074\(ra\fR. As of \fBnpm@4.0.0\fR, a new event has been introduced, \fBprepare\fR, that preserves this existing behavior. A \fInew\fR event, \fBprepublishOnly\fR has been added as a transitional strategy to allow users to avoid the confusing behavior of existing npm versions and only run on \fBnpm publish\fR (for instance, running the tests one last time to ensure they're in good shape).
95.P
96See \fI\(lahttps://github.com/npm/npm/issues/10074\(ra\fR for a much lengthier justification, with further reading, for this change.
97.P
98\fBUse Cases\fR
99.P
100Use a \fBprepare\fR script to perform build tasks that are platform-independent and need to run before your package is used. This includes tasks such as:
101.RS 0
102.IP \(bu 4
103Compiling TypeScript or other source code into JavaScript.
104.IP \(bu 4
105Creating minified versions of JavaScript source code.
106.IP \(bu 4
107Fetching remote resources that your package will use.
108.RE 0
109
110.P
111Running these build tasks in the \fBprepare\fR script ensures they happen once, in a single place, reducing complexity and variability. Additionally, this means that:
112.RS 0
113.IP \(bu 4
114You can depend on build tools as \fBdevDependencies\fR, and thus your users don't need to have them installed.
115.IP \(bu 4
116You don't need to include minifiers in your package, reducing the size for your users.
117.IP \(bu 4
118You don't need to rely on your users having \fBcurl\fR or \fBwget\fR or other system tools on the target machines.
119.RE 0
120
121.SS "Dependencies"
122.P
123The \fBdependencies\fR script is run any time an \fBnpm\fR command causes changes to the \fBnode_modules\fR directory. It is run AFTER the changes have been applied and the \fBpackage.json\fR and \fBpackage-lock.json\fR files have been updated.
124.SS "Life Cycle Operation Order"
125.SS "npm help \"cache add\""
126.RS 0
127.IP \(bu 4
128\fBprepare\fR
129.RE 0
130
131.SS "npm help ci"
132.RS 0
133.IP \(bu 4
134\fBpreinstall\fR
135.IP \(bu 4
136\fBinstall\fR
137.IP \(bu 4
138\fBpostinstall\fR
139.IP \(bu 4
140\fBprepublish\fR
141.IP \(bu 4
142\fBpreprepare\fR
143.IP \(bu 4
144\fBprepare\fR
145.IP \(bu 4
146\fBpostprepare\fR
147.RE 0
148
149.P
150These all run after the actual installation of modules into \fBnode_modules\fR, in order, with no internal actions happening in between
151.SS "npm help diff"
152.RS 0
153.IP \(bu 4
154\fBprepare\fR
155.RE 0
156
157.SS "npm help install"
158.P
159These also run when you run \fBnpm install -g <pkg-name>\fR
160.RS 0
161.IP \(bu 4
162\fBpreinstall\fR
163.IP \(bu 4
164\fBinstall\fR
165.IP \(bu 4
166\fBpostinstall\fR
167.IP \(bu 4
168\fBprepublish\fR
169.IP \(bu 4
170\fBpreprepare\fR
171.IP \(bu 4
172\fBprepare\fR
173.IP \(bu 4
174\fBpostprepare\fR
175.RE 0
176
177.P
178If there is a \fBbinding.gyp\fR file in the root of your package and you haven't defined your own \fBinstall\fR or \fBpreinstall\fR scripts, npm will default the \fBinstall\fR command to compile using node-gyp via \fBnode-gyp rebuild\fR
179.P
180These are run from the scripts of \fB<pkg-name>\fR
181.SS "npm help pack"
182.RS 0
183.IP \(bu 4
184\fBprepack\fR
185.IP \(bu 4
186\fBprepare\fR
187.IP \(bu 4
188\fBpostpack\fR
189.RE 0
190
191.SS "npm help publish"
192.RS 0
193.IP \(bu 4
194\fBprepublishOnly\fR
195.IP \(bu 4
196\fBprepack\fR
197.IP \(bu 4
198\fBprepare\fR
199.IP \(bu 4
200\fBpostpack\fR
201.IP \(bu 4
202\fBpublish\fR
203.IP \(bu 4
204\fBpostpublish\fR
205.RE 0
206
207.SS "npm help rebuild"
208.RS 0
209.IP \(bu 4
210\fBpreinstall\fR
211.IP \(bu 4
212\fBinstall\fR
213.IP \(bu 4
214\fBpostinstall\fR
215.IP \(bu 4
216\fBprepare\fR
217.RE 0
218
219.P
220\fBprepare\fR is only run if the current directory is a symlink (e.g. with linked packages)
221.SS "npm help restart"
222.P
223If there is a \fBrestart\fR script defined, these events are run; otherwise, \fBstop\fR and \fBstart\fR are both run if present, including their \fBpre\fR and \fBpost\fR iterations)
224.RS 0
225.IP \(bu 4
226\fBprerestart\fR
227.IP \(bu 4
228\fBrestart\fR
229.IP \(bu 4
230\fBpostrestart\fR
231.RE 0
232
233.SS "\fB\[rs]fBnpm run <user defined>\[rs]fR\fR \fI\(la/commands/npm-run\(ra\fR"
234.RS 0
235.IP \(bu 4
236\fBpre<user-defined>\fR
237.IP \(bu 4
238\fB<user-defined>\fR
239.IP \(bu 4
240\fBpost<user-defined>\fR
241.RE 0
242
243.SS "npm help start"
244.RS 0
245.IP \(bu 4
246\fBprestart\fR
247.IP \(bu 4
248\fBstart\fR
249.IP \(bu 4
250\fBpoststart\fR
251.RE 0
252
253.P
254If there is a \fBserver.js\fR file in the root of your package, then npm will default the \fBstart\fR command to \fBnode server.js\fR. \fBprestart\fR and \fBpoststart\fR will still run in this case.
255.SS "npm help stop"
256.RS 0
257.IP \(bu 4
258\fBprestop\fR
259.IP \(bu 4
260\fBstop\fR
261.IP \(bu 4
262\fBpoststop\fR
263.RE 0
264
265.SS "npm help test"
266.RS 0
267.IP \(bu 4
268\fBpretest\fR
269.IP \(bu 4
270\fBtest\fR
271.IP \(bu 4
272\fBposttest\fR
273.RE 0
274
275.SS "npm help version"
276.RS 0
277.IP \(bu 4
278\fBpreversion\fR
279.IP \(bu 4
280\fBversion\fR
281.IP \(bu 4
282\fBpostversion\fR
283.RE 0
284
285.SS "A Note on a lack of npm help uninstall scripts"
286.P
287While npm v6 had \fBuninstall\fR 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.
288.P
289Reasons for a package removal include:
290.RS 0
291.IP \(bu 4
292a user directly uninstalled this package
293.IP \(bu 4
294a user uninstalled a dependent package and so this dependency is being uninstalled
295.IP \(bu 4
296a user uninstalled a dependent package but another package also depends on this version
297.IP \(bu 4
298this version has been merged as a duplicate with another version
299.IP \(bu 4
300etc.
301.RE 0
302
303.P
304Due to the lack of necessary context, \fBuninstall\fR lifecycle scripts are not implemented and will not function.
305.SS "Working Directory for Scripts"
306.P
307Scripts are always run from the root of the package folder, regardless of what the current working directory is when \fBnpm\fR is invoked. This means your scripts can reliably assume they are running in the package root.
308.P
309If you want your script to behave differently based on the directory you were in when you ran \fBnpm\fR, you can use the \fBINIT_CWD\fR environment variable, which holds the full path you were in when you ran \fBnpm run\fR.
310.SS "Historical Behavior in Older npm Versions"
311.P
312For npm v6 and earlier, scripts were generally run from the root of the package, but there were rare cases and bugs in older versions where this was not guaranteed. If your package must support very old npm versions, you may wish to add a safeguard in your scripts (for example, by checking process.cwd()).
313.P
314For more details, see:
315.RS 0
316.IP \(bu 4
317\fBnpm v7 release notes\fR \fI\(lahttps://github.com/npm/cli/releases/tag/v7.0.0\(ra\fR
318.IP \(bu 4
319\fBDiscussion about script working directory reliability in npm v6 and earlier\fR \fI\(lahttps://github.com/npm/npm/issues/12356\(ra\fR
320.RE 0
321
322.SS "Environment"
323.P
324Package scripts run in an environment where many pieces of information are made available regarding the setup of npm and the current state of the process.
325.SS "path"
326.P
327If you depend on modules that define executable scripts, like test suites, then those executables will be added to the \fBPATH\fR for executing the scripts. So, if your package.json has this:
328.P
329.RS 2
330.nf
331{
332 "name" : "foo",
333 "dependencies" : {
334 "bar" : "0.1.x"
335 },
336 "scripts": {
337 "start" : "bar ./test"
338 }
339}
340.fi
341.RE
342.P
343then you could run \fBnpm start\fR to execute the \fBbar\fR script, which is exported into the \fBnode_modules/.bin\fR directory on \fBnpm install\fR.
344.SS "package.json vars"
345.P
346npm sets the following environment variables from the package.json:
347.RS 0
348.IP \(bu 4
349\fBnpm_package_name\fR - The package name
350.IP \(bu 4
351\fBnpm_package_version\fR - The package version
352.IP \(bu 4
353\fBnpm_package_bin_*\fR - Each executable defined in the bin field
354.IP \(bu 4
355\fBnpm_package_engines_*\fR - Each engine defined in the engines field
356.IP \(bu 4
357\fBnpm_package_config_*\fR - Each config value defined in the config field
358.IP \(bu 4
359\fBnpm_package_json\fR - The full path to the package.json file
360.RE 0
361
362.P
363Additionally, for install scripts (\fBpreinstall\fR, \fBinstall\fR, \fBpostinstall\fR), npm sets these environment variables:
364.RS 0
365.IP \(bu 4
366\fBnpm_package_resolved\fR - The resolved URL for the package
367.IP \(bu 4
368\fBnpm_package_integrity\fR - The integrity hash for the package
369.IP \(bu 4
370\fBnpm_package_optional\fR - Set to \fB"true"\fR if the package is optional
371.IP \(bu 4
372\fBnpm_package_dev\fR - Set to \fB"true"\fR if the package is a dev dependency
373.IP \(bu 4
374\fBnpm_package_peer\fR - Set to \fB"true"\fR if the package is a peer dependency
375.IP \(bu 4
376\fBnpm_package_dev_optional\fR - Set to \fB"true"\fR if the package is both dev and optional
377.RE 0
378
379.P
380For example, if you had \fB{"name":"foo", "version":"1.2.5"}\fR in your package.json file, then your package scripts would have the \fBnpm_package_name\fR environment variable set to "foo", and the \fBnpm_package_version\fR set to "1.2.5". You can access these variables in your code with \fBprocess.env.npm_package_name\fR and \fBprocess.env.npm_package_version\fR.
381.P
382\fBNote:\fR In npm 7 and later, most package.json fields are no longer provided as environment variables. Scripts that need access to other package.json fields should read the package.json file directly. The \fBnpm_package_json\fR environment variable provides the path to the file for this purpose.
383.P
384See \fB\[rs]fBpackage.json\[rs]fR\fR \fI\(la/configuring-npm/package-json\(ra\fR for more on package configs.
385.SS "current lifecycle event"
386.P
387Lastly, the \fBnpm_lifecycle_event\fR environment variable is set to whichever stage of the cycle is being executed. So, you could have a single script used for different parts of the process which switches based on what's currently happening.
388.P
389Objects are flattened following this format, so if you had \fB{"scripts":{"install":"foo.js"}}\fR in your package.json, then you'd see this in the script:
390.P
391.RS 2
392.nf
393process.env.npm_package_scripts_install === "foo.js"
394.fi
395.RE
396.SS "Examples"
397.P
398For example, if your package.json contains this:
399.P
400.RS 2
401.nf
402{
403 "scripts" : {
404 "prepare" : "scripts/build.js",
405 "test" : "scripts/test.js"
406 }
407}
408.fi
409.RE
410.P
411then \fBscripts/build.js\fR will be called for the prepare stage of the lifecycle, and you can check the \fBnpm_lifecycle_event\fR environment variable if your script needs to behave differently in different contexts.
412.P
413If you want to run build commands, you can do so. This works just fine:
414.P
415.RS 2
416.nf
417{
418 "scripts" : {
419 "prepare" : "npm run build",
420 "build" : "tsc",
421 "test" : "jest"
422 }
423}
424.fi
425.RE
426.SS "Exiting"
427.P
428Scripts are run by passing the line as a script argument to \fB/bin/sh\fR on POSIX systems or \fBcmd.exe\fR on Windows. You can control which shell is used by setting the \fB\[rs]fBscript-shell\[rs]fR\fR \fI\(la/using-npm/config#script-shell\(ra\fR configuration option.
429.P
430If the script exits with a code other than 0, then this will abort the process.
431.P
432Note that these script files don't have to be Node.js or even JavaScript programs. They just have to be some kind of executable file.
433.SS "Best Practices"
434.RS 0
435.IP \(bu 4
436Don't exit with a non-zero error code unless you \fIreally\fR mean it. If the failure is minor or only will prevent some optional features, then it's better to just print a warning and exit successfully.
437.IP \(bu 4
438Try not to use scripts to do what npm can do for you. Read through \fB\[rs]fBpackage.json\[rs]fR\fR \fI\(la/configuring-npm/package-json\(ra\fR to see all the things that you can specify and enable by simply describing your package appropriately. In general, this will lead to a more robust and consistent state.
439.IP \(bu 4
440Inspect the env to determine where to put things. For instance, if the \fBNPM_CONFIG_BINROOT\fR environment variable is set to \fB/home/user/bin\fR, then don't try to install executables into \fB/usr/local/bin\fR. The user probably set it up that way for a reason.
441.IP \(bu 4
442Don't prefix your script commands with "sudo". If root permissions are required for some reason, then it'll fail with that error, and the user will sudo the npm command in question.
443.IP \(bu 4
444Don't use \fBinstall\fR. Use a \fB.gyp\fR file for compilation, and \fBprepare\fR for anything else. You should almost never have to explicitly set a preinstall or install script. If you are doing this, please consider if there is another option. The only valid use of \fBinstall\fR or \fBpreinstall\fR scripts is for compilation which must be done on the target architecture.
445.RE 0
446
447.SS "See Also"
448.RS 0
449.IP \(bu 4
450npm help run
451.IP \(bu 4
452\fBpackage.json\fR \fI\(la/configuring-npm/package-json\(ra\fR
453.IP \(bu 4
454npm help developers
455.IP \(bu 4
456npm help install
457.RE 0
458 