Skip to content
Stars Background

Packages can define in the scripts field of their manifest various actions that should be executed when the package manager executes a particular workflow.

Those script are called right at the beginning and the end of each call to yarn pack. They are respectively meant to turn your package from development into production, and cleanup any lingering artifact. For instance, a typical prepack script would call Babel or TypeScript on the source directory to turn .ts files into .js files.

This script is called before yarn npm publish before the package has even been packed. This is the place where you’ll want to check that the project is in an ok state.

This script is called after the package dependency tree changed in any way — usually after a dependency (or transitive dependency) got added, removed, or updated, but also sometimes when the project configuration or environment changed (for example when changing the Node.js version).

It is guaranteed to be called in topological order (in other words, your dependencies’ postinstall scripts will always run before yours).

For backwards compatibility, the preinstall and install scripts, if presents, are called right before running the postinstall script from the same package. In general, prefer using postinstall over those two.

When running scripts and binaries, some environment variables are usually made available:

VariableDescription
$INIT_CWDDirectory from which the script has been invoked. This isn’t the same as the cwd, which for scripts is always equal to the closest package root.
$PROJECT_CWDRoot of the project on the filesystem.
$npm_package_nameName of the running package.
$npm_package_versionVersion of the running package.
$npm_package_jsonAbsolute path to the package.json of the running package.
$npm_execpathAbsolute path to the Yarn binary.
$npm_node_execpathAbsolute path to the Node binary.
$npm_config_user_agentString defining the Yarn version currently in use.
$npm_lifecycle_eventName of the script or lifecycle event, if relevant.