2020-11-01 07:54:36 +01:00
.TH "SCRIPTS" "7" "November 2020" "" ""
2011-11-26 09:21:03 -08:00
.SH "NAME"
2019-11-18 21:01:39 +02:00
\fB scripts\fR \- How npm handles the "scripts" field
2019-11-05 14:55:08 -05:00
.SS Description
2014-09-24 14:41:07 -07:00
.P
2020-02-26 17:11:08 -08:00
The \fB "scripts"\fP property of of your \fB package\. json\fP 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 \fB npm run\- script <stage>\fP or \fB npm run <stage>\fP for short\. \fI Pre\fR and \fI post\fR commands with matching names will be run for those as well (e\. g\. \fB premyscript\fP , \fB myscript\fP , \fB postmyscript\fP )\. Scripts from dependencies can be run with \fB npm explore <pkg> \- \- npm run <stage>\fP \| \.
.SS Pre & Post Scripts
.P
To create "pre" or "post" scripts for any scripts defined in the \fB "scripts"\fP section of the \fB package\. json\fP , simply create another script \fI with a matching name\fR and add "pre" or "post" to the beginning of them\.
.P
.RS 2
.nf
{
"scripts": {
"precompress": "{{ executes BEFORE the `compress` script }}",
"compress": "{{ run command to compress files }}",
"postcompress": "{{ executes AFTER `compress` script }}"
}
}
.fi
.RE
.SS Life Cycle Scripts
.P
There are some special life cycle scripts that happen only in certain situations\. These scripts happen in addtion to the "pre" and "post" script\.
2014-09-24 14:41:07 -07:00
.RS 0
.IP \(bu 2
2020-02-26 17:11:08 -08:00
\fB prepare\fP , \fB prepublish\fP , \fB prepublishOnly\fP , \fB prepack\fP , \fB postpack\fP
.RE
.P
\fB prepare\fR (since \fB npm@4\. 0\. 0\fP )
.RS 0
2014-09-24 14:41:07 -07:00
.IP \(bu 2
2020-02-26 17:11:08 -08:00
Runs BEFORE the package is packed
2014-09-24 14:41:07 -07:00
.IP \(bu 2
2020-02-26 17:11:08 -08:00
Runs BEFORE the package is published
2014-09-24 14:41:07 -07:00
.IP \(bu 2
2020-02-26 17:11:08 -08:00
Runs on local \fB npm install\fP without any arguments
2016-09-22 07:59:37 -07:00
.IP \(bu 2
2020-02-26 17:11:08 -08:00
Run AFTER \fB prepublish\fP , but BEFORE \fB prepublishOnly\fP
2015-05-28 22:27:26 -04:00
.IP \(bu 2
2020-02-26 17:11:08 -08:00
NOTE: If a package being installed through git contains a \fB prepare\fP script, its \fB dependencies\fP and \fB devDependencies\fP will be installed, and the prepare script will be run, before the package is packaged and installed\.
.RE
.P
\fB prepublish\fR (DEPRECATED)
.RS 0
2015-05-28 22:27:26 -04:00
.IP \(bu 2
2020-02-26 17:11:08 -08:00
Same as \fB prepare\fP
.RE
.P
\fB prepublishOnly\fR
.RS 0
2014-09-24 14:41:07 -07:00
.IP \(bu 2
2020-02-26 17:11:08 -08:00
Runs BEFORE the package is prepared and packed, ONLY on \fB npm publish\fP \| \.
.RE
.P
\fB prepack\fR
.RS 0
2014-09-24 14:41:07 -07:00
.IP \(bu 2
2020-02-26 17:11:08 -08:00
Runs BEFORE a tarball is packed (on "\fB npm pack\fP ", "\fB npm publish\fP ", and when installing a git dependencies)\.
2014-09-24 14:41:07 -07:00
.IP \(bu 2
2020-02-26 17:11:08 -08:00
NOTE: "\fB npm run pack\fP " is NOT the same as "\fB npm pack\fP "\. "\fB npm run pack\fP " is an arbitrary user defined script name, where as, "\fB npm pack\fP " is a CLI defined command\.
.RE
.P
\fB postpack\fR
.RS 0
2016-06-24 13:43:51 -07:00
.IP \(bu 2
2020-02-26 17:11:08 -08:00
Runs AFTER the tarball has been generated and moved to its final destination\.
2014-09-24 14:41:07 -07:00
.RE
2020-02-26 17:11:08 -08:00
.SS Prepare and Prepublish
.P
\fB Deprecation Note: prepublish\fR
2014-09-24 14:41:07 -07:00
.P
2020-02-26 17:11:08 -08:00
Since \fB npm@1\. 1\. 71\fP , the npm CLI has run the \fB prepublish\fP script for both \fB npm publish\fP and \fB npm install\fP , 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 \fI https://github\. com/npm/npm/issues/10074\fR \| \. As of \fB npm@4\. 0\. 0\fP , a new event has been introduced, \fB prepare\fP , that preserves this existing behavior\. A \fI new\fR event, \fB prepublishOnly\fP has been added as a transitional strategy to allow users to avoid the confusing behavior of existing npm versions and only run on \fB npm publish\fP (for instance, running the tests one last time to ensure they're in good shape)\.
.P
See https://github\. com/npm/npm/issues/10074 for a much lengthier justification, with further reading, for this change\.
.P
\fB Use Cases\fR
.P
If 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 \fB prepublish\fP script\. This includes tasks such as:
2014-09-24 14:41:07 -07:00
.RS 0
.IP \(bu 2
2015-03-06 02:57:32 -06:00
Compiling CoffeeScript source code into JavaScript\.
2014-09-24 14:41:07 -07:00
.IP \(bu 2
2015-03-06 02:57:32 -06:00
Creating minified versions of JavaScript source code\.
2014-09-24 14:41:07 -07:00
.IP \(bu 2
2013-01-09 15:21:30 -08:00
Fetching remote resources that your package will use\.
2014-09-24 14:41:07 -07:00
.RE
2013-01-09 15:21:30 -08:00
.P
2020-02-26 17:11:08 -08:00
The advantage of doing these things at \fB prepublish\fP time is that they can be done once, in a single place, thus reducing complexity and variability\. Additionally, this means that:
2014-09-24 14:41:07 -07:00
.RS 0
.IP \(bu 2
2015-07-24 15:09:52 -07:00
You can depend on \fB coffee\- script\fP as a \fB devDependency\fP , and thus
2014-09-24 14:41:07 -07:00
your users don't need to have it installed\.
.IP \(bu 2
2015-03-06 02:57:32 -06:00
You don't need to include minifiers in your package, reducing
2013-01-09 15:21:30 -08:00
the size for your users\.
2014-09-24 14:41:07 -07:00
.IP \(bu 2
2015-07-24 15:09:52 -07:00
You don't need to rely on your users having \fB curl\fP or \fB wget\fP or
2013-01-09 15:21:30 -08:00
other system tools on the target machines\.
2014-09-24 14:41:07 -07:00
2020-02-26 17:11:08 -08:00
.RE
.SS Life Cycle Operation Order
.SS npm help \fB publish\fP
.RS 0
.IP \(bu 2
\fB prepublishOnly\fP
.IP \(bu 2
\fB prepare\fP
.IP \(bu 2
\fB prepublish\fP
.IP \(bu 2
\fB publish\fP
.IP \(bu 2
\fB postpublish\fP
.RE
.SS npm help \fB pack\fP
.RS 0
.IP \(bu 2
\fB prepack\fP
.IP \(bu 2
\fB postpack\fP
.RE
.SS npm help \fB install\fP
.RS 0
.IP \(bu 2
\fB preinstall\fP
.IP \(bu 2
\fB install\fP
.IP \(bu 2
\fB postinstall\fP
.RE
.P
Also triggers
.RS 0
.IP \(bu 2
\fB prepublish\fP (when on local)
.IP \(bu 2
2020-10-15 20:32:02 -04:00
\fB prepare\fP (when on local or workspaces)
2020-02-26 17:11:08 -08:00
.RE
.SS npm help \fB start\fP
.P
\fB npm run start\fP has an \fB npm start\fP shorthand\.
.RS 0
.IP \(bu 2
\fB prestart\fP
.IP \(bu 2
\fB start\fP
.IP \(bu 2
\fB poststart\fP
2014-09-24 14:41:07 -07:00
.RE
2019-11-05 14:55:08 -05:00
.SS Default Values
2014-09-24 14:41:07 -07:00
.P
2011-11-26 09:21:03 -08:00
npm will default some script values based on package contents\.
2014-09-24 14:41:07 -07:00
.RS 0
.IP \(bu 2
2015-07-24 15:09:52 -07:00
\fB "start": "node server\. js"\fP :
If there is a \fB server\. js\fP file in the root of your package, then npm
will default the \fB start\fP command to \fB node server\. js\fP \| \.
2014-09-24 14:41:07 -07:00
.IP \(bu 2
2015-10-09 11:36:19 -07:00
\fB "install": "node\- gyp rebuild"\fP :
2016-05-27 14:07:59 -07:00
If there is a \fB binding\. gyp\fP file in the root of your package and you
haven't defined your own \fB install\fP or \fB preinstall\fP scripts, npm will
2015-10-09 11:36:19 -07:00
default the \fB install\fP command to compile using node\- gyp\.
2014-09-24 14:41:07 -07:00
.RE
2019-11-05 14:55:08 -05:00
.SS User
2014-09-24 14:41:07 -07:00
.P
2013-07-12 08:55:57 -07:00
If npm was invoked with root privileges, then it will change the uid
2015-07-24 15:09:52 -07:00
to the user account or uid specified by the \fB user\fP config, which
defaults to \fB nobody\fP \| \. Set the \fB unsafe\- perm\fP flag to run scripts with
2013-07-12 08:55:57 -07:00
root privileges\.
2019-11-05 14:55:08 -05:00
.SS Environment
2014-09-24 14:41:07 -07:00
.P
2013-07-12 08:55:57 -07:00
Package 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\.
2014-09-24 14:41:07 -07:00
.SS path
.P
2013-07-12 08:55:57 -07:00
If you depend on modules that define executable scripts, like test
2015-07-24 15:09:52 -07:00
suites, then those executables will be added to the \fB PATH\fP for
2013-07-12 08:55:57 -07:00
executing the scripts\. So, if your package\. json has this:
2014-09-24 14:41:07 -07:00
.P
.RS 2
2014-11-04 15:08:12 -08:00
.nf
2020-11-03 20:39:24 -05:00
{
"name" : "foo",
"dependencies" : {
"bar" : "0\. 1\. x"
},
"scripts": {
"start" : "bar \. /test"
}
2020-05-04 14:57:47 -04:00
}
2014-11-04 15:08:12 -08:00
.fi
2014-09-24 14:41:07 -07:00
.RE
2012-05-05 22:33:06 -07:00
.P
2015-07-24 15:09:52 -07:00
then you could run \fB npm start\fP to execute the \fB bar\fP script, which is
exported into the \fB node_modules/\. bin\fP directory on \fB npm install\fP \| \.
2014-09-24 14:41:07 -07:00
.SS package\.json vars
.P
2015-07-24 15:09:52 -07:00
The package\. json fields are tacked onto the \fB npm_package_\fP prefix\. So,
for instance, if you had \fB {"name":"foo", "version":"1\. 2\. 5"}\fP in your
2014-09-24 14:41:07 -07:00
package\. json file, then your package scripts would have the
2015-07-24 15:09:52 -07:00
\fB npm_package_name\fP environment variable set to "foo", and the
2019-11-18 21:01:39 +02:00
\fB npm_package_version\fP set to "1\. 2\. 5"\. You can access these variables
in your code with \fB process\. env\. npm_package_name\fP and
2018-08-29 12:03:09 -07:00
\fB process\. env\. npm_package_version\fP , and so on for other fields\.
2014-09-24 14:41:07 -07:00
.SS configuration
.P
Configuration parameters are put in the environment with the
2015-07-24 15:09:52 -07:00
\fB npm_config_\fP prefix\. For instance, you can view the effective \fB root\fP
config by checking the \fB npm_config_root\fP environment variable\.
2014-11-04 15:08:12 -08:00
.SS Special: package\.json "config" object
2014-09-24 14:41:07 -07:00
.P
2011-11-26 09:21:03 -08:00
The package\. json "config" keys are overwritten in the environment if
2015-07-24 15:09:52 -07:00
there is a config param of \fB <name>[@<version>]:<key>\fP \| \. For example,
2013-07-12 08:55:57 -07:00
if the package\. json has this:
2014-09-24 14:41:07 -07:00
.P
.RS 2
2014-11-04 15:08:12 -08:00
.nf
2020-11-03 20:39:24 -05:00
{
"name" : "foo",
"config" : {
"port" : "8080"
},
"scripts" : {
"start" : "node server\. js"
}
2020-05-04 14:57:47 -04:00
}
2014-11-04 15:08:12 -08:00
.fi
2014-09-24 14:41:07 -07:00
.RE
2011-11-26 09:21:03 -08:00
.P
and the server\. js is this:
2014-09-24 14:41:07 -07:00
.P
.RS 2
2014-11-04 15:08:12 -08:00
.nf
2011-11-26 09:21:03 -08:00
http\. createServer(\. \. \. )\. listen(process\. env\. npm_package_config_port)
2014-11-04 15:08:12 -08:00
.fi
2014-09-24 14:41:07 -07:00
.RE
2011-11-26 09:21:03 -08:00
.P
then the user could change the behavior by doing:
2014-09-24 14:41:07 -07:00
.P
.RS 2
2014-11-04 15:08:12 -08:00
.nf
2019-11-05 14:55:08 -05:00
npm config set foo:port 80
2014-11-04 15:08:12 -08:00
.fi
2014-09-24 14:41:07 -07:00
.RE
.SS current lifecycle event
.P
2015-07-24 15:09:52 -07:00
Lastly, the \fB npm_lifecycle_event\fP environment variable is set to
2013-07-12 08:55:57 -07:00
whichever stage of the cycle is being executed\. So, you could have a
single script used for different parts of the process which switches
2014-09-24 14:41:07 -07:00
based on what's currently happening\.
2011-11-26 09:21:03 -08:00
.P
2014-09-24 14:41:07 -07:00
Objects are flattened following this format, so if you had
2015-07-24 15:09:52 -07:00
\fB {"scripts":{"install":"foo\. js"}}\fP in your package\. json, then you'd
2013-07-12 08:55:57 -07:00
see this in the script:
2014-09-24 14:41:07 -07:00
.P
.RS 2
2014-11-04 15:08:12 -08:00
.nf
2011-11-26 09:21:03 -08:00
process\. env\. npm_package_scripts_install === "foo\. js"
2014-11-04 15:08:12 -08:00
.fi
2014-09-24 14:41:07 -07:00
.RE
2019-11-05 14:55:08 -05:00
.SS Examples
2014-09-24 14:41:07 -07:00
.P
2011-11-26 09:21:03 -08:00
For example, if your package\. json contains this:
2014-09-24 14:41:07 -07:00
.P
.RS 2
2014-11-04 15:08:12 -08:00
.nf
2020-11-03 20:39:24 -05:00
{
"scripts" : {
"install" : "scripts/install\. js",
"postinstall" : "scripts/postinstall\. js",
2020-05-04 14:57:47 -04:00
"uninstall" : "scripts/uninstall\. js"
2011-11-26 09:21:03 -08:00
}
}
2014-11-04 15:08:12 -08:00
.fi
2014-09-24 14:41:07 -07:00
.RE
2011-11-26 09:21:03 -08:00
.P
2016-01-28 18:11:35 -08:00
then \fB scripts/install\. js\fP will be called for the install
and post\- install stages of the lifecycle, and \fB scripts/uninstall\. js\fP
will be called when the package is uninstalled\. Since
\fB scripts/install\. js\fP is running for two different phases, it would
2015-07-24 15:09:52 -07:00
be wise in this case to look at the \fB npm_lifecycle_event\fP environment
2013-07-12 08:55:57 -07:00
variable\.
2011-11-26 09:21:03 -08:00
.P
2013-07-12 08:55:57 -07:00
If you want to run a make command, you can do so\. This works just
fine:
2014-09-24 14:41:07 -07:00
.P
.RS 2
2014-11-04 15:08:12 -08:00
.nf
2020-11-03 20:39:24 -05:00
{
"scripts" : {
"preinstall" : "\. /configure",
"install" : "make && make install",
2020-05-04 14:57:47 -04:00
"test" : "make test"
2011-11-26 09:21:03 -08:00
}
}
2014-11-04 15:08:12 -08:00
.fi
2014-09-24 14:41:07 -07:00
.RE
2019-11-05 14:55:08 -05:00
.SS Exiting
2014-09-24 14:41:07 -07:00
.P
2015-07-24 15:09:52 -07:00
Scripts are run by passing the line as a script argument to \fB sh\fP \| \.
2011-11-26 09:21:03 -08:00
.P
If the script exits with a code other than 0, then this will abort the
process\.
.P
2014-09-24 14:41:07 -07:00
Note that these script files don't have to be nodejs or even
2013-07-12 08:55:57 -07:00
javascript programs\. They just have to be some kind of executable
file\.
2019-11-05 14:55:08 -05:00
.SS Hook Scripts
2014-09-24 14:41:07 -07:00
.P
2013-07-12 08:55:57 -07:00
If you want to run a specific script at a specific lifecycle event for
ALL packages, then you can use a hook script\.
2011-11-26 09:21:03 -08:00
.P
2015-07-24 15:09:52 -07:00
Place an executable file at \fB node_modules/\. hooks/{eventname}\fP , and
2014-09-24 14:41:07 -07:00
it'll get run for all packages when they are going through that point
2013-07-12 08:55:57 -07:00
in the package lifecycle for any packages installed in that root\.
2011-11-26 09:21:03 -08:00
.P
2013-07-12 08:55:57 -07:00
Hook scripts are run exactly the same way as package\. json scripts\.
That is, they are in a separate child process, with the env described
above\.
2019-11-05 14:55:08 -05:00
.SS Best Practices
2014-09-24 14:41:07 -07:00
.RS 0
.IP \(bu 2
Don't exit with a non\- zero error code unless you \fI really\fR mean it\.
2013-07-12 08:55:57 -07:00
Except for uninstall scripts, this will cause the npm action to
fail, and potentially be rolled back\. If the failure is minor or
2014-09-24 14:41:07 -07:00
only will prevent some optional features, then it's better to just
2011-11-26 09:21:03 -08:00
print a warning and exit successfully\.
2014-09-24 14:41:07 -07:00
.IP \(bu 2
Try not to use scripts to do what npm can do for you\. Read through
2019-11-18 21:01:39 +02:00
npm help \fB package\. json\fP to see all the things that you can specify and enable
2013-07-12 08:55:57 -07:00
by simply describing your package appropriately\. In general, this
will lead to a more robust and consistent state\.
2014-09-24 14:41:07 -07:00
.IP \(bu 2
2011-11-26 09:21:03 -08:00
Inspect the env to determine where to put things\. For instance, if
2016-01-28 18:11:35 -08:00
the \fB npm_config_binroot\fP environment variable is set to \fB /home/user/bin\fP , then
2015-07-24 15:09:52 -07:00
don't try to install executables into \fB /usr/local/bin\fP \| \. The user
2013-07-12 08:55:57 -07:00
probably set it up that way for a reason\.
2014-09-24 14:41:07 -07:00
.IP \(bu 2
Don't prefix your script commands with "sudo"\. If root permissions
are required for some reason, then it'll fail with that error, and
2013-07-12 08:55:57 -07:00
the user will sudo the npm command in question\.
2015-03-06 02:57:32 -06:00
.IP \(bu 2
2015-07-24 15:09:52 -07:00
Don't use \fB install\fP \| \. Use a \fB \| \. gyp\fP file for compilation, and \fB prepublish\fP
2015-03-06 02:57:32 -06:00
for anything else\. You should almost never have to explicitly set a
preinstall or install script\. If you are doing this, please consider if
2015-07-24 15:09:52 -07:00
there is another option\. The only valid use of \fB install\fP or \fB preinstall\fP
2015-03-06 02:57:32 -06:00
scripts is for compilation which must be done on the target architecture\.
2014-09-24 14:41:07 -07:00
.RE
2019-11-05 14:55:08 -05:00
.SS See Also
2014-09-24 14:41:07 -07:00
.RS 0
.IP \(bu 2
2019-11-18 21:01:39 +02:00
npm help run\- script
2014-09-24 14:41:07 -07:00
.IP \(bu 2
2019-11-18 21:01:39 +02:00
npm help package\. json
2014-09-24 14:41:07 -07:00
.IP \(bu 2
2019-11-05 14:55:08 -05:00
npm help developers
2014-09-24 14:41:07 -07:00
.IP \(bu 2
2019-11-18 21:01:39 +02:00
npm help install
2014-09-24 14:41:07 -07:00
.RE