.TH "SCRIPTS" "7" "March 2026" "NPM@11.12.0" ""
.SH "NAME"
\fBScripts\fR - How npm handles the "scripts" field
.SS "Description"
.P
The \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.
.SS "Pre & Post Scripts"
.P
To 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.
.P
.RS 2
.nf
{
  "scripts": {
    "precompress": "{{ executes BEFORE the `compress` script }}",
    "compress": "{{ run command to compress files }}",
    "postcompress": "{{ executes AFTER `compress` script }}"
  }
}
.fi
.RE
.P
In this example \fBnpm run compress\fR would execute these scripts as described.
.SS "Life Cycle Scripts"
.P
There 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.
.RS 0
.IP \(bu 4
\fBprepare\fR, \fBprepublish\fR, \fBprepublishOnly\fR, \fBprepack\fR, \fBpostpack\fR, \fBdependencies\fR
.RE 0

.P
\fBprepare\fR (since \fBnpm@4.0.0\fR)
.RS 0
.IP \(bu 4
Runs BEFORE the package is packed, i.e. during \fBnpm publish\fR and \fBnpm pack\fR
.IP \(bu 4
Runs 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)
.IP \(bu 4
Runs AFTER \fBprepublishOnly\fR and \fBprepack\fR, but BEFORE \fBpostpack\fR
.IP \(bu 4
Runs for a package if it's being installed as a link through \fBnpm install <folder>\fR
.IP \(bu 4
NOTE: 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.
.IP \(bu 4
As of \fBnpm@7\fR these scripts run in the background. To see the output, run with: \fB--foreground-scripts\fR.
.IP \(bu 4
\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.
.RE 0

.P
\fBprepublish\fR (DEPRECATED)
.RS 0
.IP \(bu 4
Does not run during \fBnpm publish\fR, but does run during \fBnpm ci\fR and \fBnpm install\fR. See below for more info.
.RE 0

.P
\fBprepublishOnly\fR
.RS 0
.IP \(bu 4
Runs BEFORE the package is prepared and packed, ONLY on \fBnpm publish\fR.
.RE 0

.P
\fBprepack\fR
.RS 0
.IP \(bu 4
Runs BEFORE a tarball is packed (on "\fBnpm pack\fR", "\fBnpm publish\fR", and when installing a git dependency).
.IP \(bu 4
NOTE: "\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.
.RE 0

.P
\fBpostpack\fR
.RS 0
.IP \(bu 4
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)
.RE 0

.P
\fBdependencies\fR
.RS 0
.IP \(bu 4
Runs AFTER any operations that modify the \fBnode_modules\fR directory IF changes occurred.
.IP \(bu 4
Does NOT run in global mode
.RE 0

.SS "Prepare and Prepublish"
.P
\fBDeprecation Note: prepublish\fR
.P
Since \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).
.P
See \fI\(lahttps://github.com/npm/npm/issues/10074\(ra\fR for a much lengthier justification, with further reading, for this change.
.P
\fBUse Cases\fR
.P
Use 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:
.RS 0
.IP \(bu 4
Compiling TypeScript or other source code into JavaScript.
.IP \(bu 4
Creating minified versions of JavaScript source code.
.IP \(bu 4
Fetching remote resources that your package will use.
.RE 0

.P
Running these build tasks in the \fBprepare\fR script ensures they happen once, in a single place, reducing complexity and variability. Additionally, this means that:
.RS 0
.IP \(bu 4
You can depend on build tools as \fBdevDependencies\fR, and thus your users don't need to have them installed.
.IP \(bu 4
You don't need to include minifiers in your package, reducing the size for your users.
.IP \(bu 4
You don't need to rely on your users having \fBcurl\fR or \fBwget\fR or other system tools on the target machines.
.RE 0

.SS "Dependencies"
.P
The \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.
.SS "Life Cycle Operation Order"
.SS "npm help \"cache add\""
.RS 0
.IP \(bu 4
\fBprepare\fR
.RE 0

.SS "npm help ci"
.RS 0
.IP \(bu 4
\fBpreinstall\fR
.IP \(bu 4
\fBinstall\fR
.IP \(bu 4
\fBpostinstall\fR
.IP \(bu 4
\fBprepublish\fR
.IP \(bu 4
\fBpreprepare\fR
.IP \(bu 4
\fBprepare\fR
.IP \(bu 4
\fBpostprepare\fR
.RE 0

.P
These all run after the actual installation of modules into \fBnode_modules\fR, in order, with no internal actions happening in between
.SS "npm help diff"
.RS 0
.IP \(bu 4
\fBprepare\fR
.RE 0

.SS "npm help install"
.P
These also run when you run \fBnpm install -g <pkg-name>\fR
.RS 0
.IP \(bu 4
\fBpreinstall\fR
.IP \(bu 4
\fBinstall\fR
.IP \(bu 4
\fBpostinstall\fR
.IP \(bu 4
\fBprepublish\fR
.IP \(bu 4
\fBpreprepare\fR
.IP \(bu 4
\fBprepare\fR
.IP \(bu 4
\fBpostprepare\fR
.RE 0

.P
If 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
.P
These are run from the scripts of \fB<pkg-name>\fR
.SS "npm help pack"
.RS 0
.IP \(bu 4
\fBprepack\fR
.IP \(bu 4
\fBprepare\fR
.IP \(bu 4
\fBpostpack\fR
.RE 0

.SS "npm help publish"
.RS 0
.IP \(bu 4
\fBprepublishOnly\fR
.IP \(bu 4
\fBprepack\fR
.IP \(bu 4
\fBprepare\fR
.IP \(bu 4
\fBpostpack\fR
.IP \(bu 4
\fBpublish\fR
.IP \(bu 4
\fBpostpublish\fR
.RE 0

.SS "npm help rebuild"
.RS 0
.IP \(bu 4
\fBpreinstall\fR
.IP \(bu 4
\fBinstall\fR
.IP \(bu 4
\fBpostinstall\fR
.IP \(bu 4
\fBprepare\fR
.RE 0

.P
\fBprepare\fR is only run if the current directory is a symlink (e.g. with linked packages)
.SS "npm help restart"
.P
If 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)
.RS 0
.IP \(bu 4
\fBprerestart\fR
.IP \(bu 4
\fBrestart\fR
.IP \(bu 4
\fBpostrestart\fR
.RE 0

.SS "\fB\[rs]fBnpm run <user defined>\[rs]fR\fR \fI\(la/commands/npm-run\(ra\fR"
.RS 0
.IP \(bu 4
\fBpre<user-defined>\fR
.IP \(bu 4
\fB<user-defined>\fR
.IP \(bu 4
\fBpost<user-defined>\fR
.RE 0

.SS "npm help start"
.RS 0
.IP \(bu 4
\fBprestart\fR
.IP \(bu 4
\fBstart\fR
.IP \(bu 4
\fBpoststart\fR
.RE 0

.P
If 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.
.SS "npm help stop"
.RS 0
.IP \(bu 4
\fBprestop\fR
.IP \(bu 4
\fBstop\fR
.IP \(bu 4
\fBpoststop\fR
.RE 0

.SS "npm help test"
.RS 0
.IP \(bu 4
\fBpretest\fR
.IP \(bu 4
\fBtest\fR
.IP \(bu 4
\fBposttest\fR
.RE 0

.SS "npm help version"
.RS 0
.IP \(bu 4
\fBpreversion\fR
.IP \(bu 4
\fBversion\fR
.IP \(bu 4
\fBpostversion\fR
.RE 0

.SS "A Note on a lack of npm help uninstall scripts"
.P
While 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.
.P
Reasons for a package removal include:
.RS 0
.IP \(bu 4
a user directly uninstalled this package
.IP \(bu 4
a user uninstalled a dependent package and so this dependency is being uninstalled
.IP \(bu 4
a user uninstalled a dependent package but another package also depends on this version
.IP \(bu 4
this version has been merged as a duplicate with another version
.IP \(bu 4
etc.
.RE 0

.P
Due to the lack of necessary context, \fBuninstall\fR lifecycle scripts are not implemented and will not function.
.SS "Working Directory for Scripts"
.P
Scripts 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.
.P
If 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.
.SS "Historical Behavior in Older npm Versions"
.P
For 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()).
.P
For more details, see:
.RS 0
.IP \(bu 4
\fBnpm v7 release notes\fR \fI\(lahttps://github.com/npm/cli/releases/tag/v7.0.0\(ra\fR
.IP \(bu 4
\fBDiscussion about script working directory reliability in npm v6 and earlier\fR \fI\(lahttps://github.com/npm/npm/issues/12356\(ra\fR
.RE 0

.SS "Environment"
.P
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.
.SS "path"
.P
If 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:
.P
.RS 2
.nf
{
  "name" : "foo",
  "dependencies" : {
    "bar" : "0.1.x"
  },
  "scripts": {
    "start" : "bar ./test"
  }
}
.fi
.RE
.P
then 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.
.SS "package.json vars"
.P
npm sets the following environment variables from the package.json:
.RS 0
.IP \(bu 4
\fBnpm_package_name\fR - The package name
.IP \(bu 4
\fBnpm_package_version\fR - The package version
.IP \(bu 4
\fBnpm_package_bin_*\fR - Each executable defined in the bin field
.IP \(bu 4
\fBnpm_package_engines_*\fR - Each engine defined in the engines field
.IP \(bu 4
\fBnpm_package_config_*\fR - Each config value defined in the config field
.IP \(bu 4
\fBnpm_package_json\fR - The full path to the package.json file
.RE 0

.P
Additionally, for install scripts (\fBpreinstall\fR, \fBinstall\fR, \fBpostinstall\fR), npm sets these environment variables:
.RS 0
.IP \(bu 4
\fBnpm_package_resolved\fR - The resolved URL for the package
.IP \(bu 4
\fBnpm_package_integrity\fR - The integrity hash for the package
.IP \(bu 4
\fBnpm_package_optional\fR - Set to \fB"true"\fR if the package is optional
.IP \(bu 4
\fBnpm_package_dev\fR - Set to \fB"true"\fR if the package is a dev dependency
.IP \(bu 4
\fBnpm_package_peer\fR - Set to \fB"true"\fR if the package is a peer dependency
.IP \(bu 4
\fBnpm_package_dev_optional\fR - Set to \fB"true"\fR if the package is both dev and optional
.RE 0

.P
For 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.
.P
\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.
.P
See \fB\[rs]fBpackage.json\[rs]fR\fR \fI\(la/configuring-npm/package-json\(ra\fR for more on package configs.
.SS "current lifecycle event"
.P
Lastly, 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.
.P
Objects 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:
.P
.RS 2
.nf
process.env.npm_package_scripts_install === "foo.js"
.fi
.RE
.SS "Examples"
.P
For example, if your package.json contains this:
.P
.RS 2
.nf
{
  "scripts" : {
    "prepare" : "scripts/build.js",
    "test" : "scripts/test.js"
  }
}
.fi
.RE
.P
then \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.
.P
If you want to run build commands, you can do so. This works just fine:
.P
.RS 2
.nf
{
  "scripts" : {
    "prepare" : "npm run build",
    "build" : "tsc",
    "test" : "jest"
  }
}
.fi
.RE
.SS "Exiting"
.P
Scripts 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.
.P
If the script exits with a code other than 0, then this will abort the process.
.P
Note 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.
.SS "Best Practices"
.RS 0
.IP \(bu 4
Don'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.
.IP \(bu 4
Try 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.
.IP \(bu 4
Inspect 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.
.IP \(bu 4
Don'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.
.IP \(bu 4
Don'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.
.RE 0

.SS "See Also"
.RS 0
.IP \(bu 4
npm help run
.IP \(bu 4
\fBpackage.json\fR \fI\(la/configuring-npm/package-json\(ra\fR
.IP \(bu 4
npm help developers
.IP \(bu 4
npm help install
.RE 0
