Techletter #86 | August 17, 2024
What is the use of preinstall
and postinstall
scripts in package.json?
The preinstall
and postinstall
scripts in package.json
are part of the npm lifecycle scripts that allow developers to execute specific commands before and after the installation of a package, respectively.
The preinstall
script is executed before the package is installed. This can be useful for several purposes, such as:
- Setting Up Environment: It can be used to prepare the environment by installing necessary dependencies or performing checks. For example, a package might need to download native libraries or set up configuration files before the main installation occurs.
- Dependency Management: Some packages, like NodeGit, utilize
preinstall
to ensure that required native dependencies are available. If the dependencies are not pre-built, the script can trigger their compilation
The postinstall
script runs immediately after the package has been installed. Its uses include:
- Finalizing Setup: This script can be used to perform additional setup tasks, such as bootstrapping workflows or configuring services.
- User Notifications: It can also serve to notify users or provide instructions after the installation is complete, although this is less common due to potential visibility issues with the output of these scripts.
What is husky?
Husky is an npm package that simplifies the use of Git hooks in JavaScript/TypeScript projects. Git hooks are scripts that Git executes automatically at certain points in the Git workflow, such as before committing or pushing changes. By using Husky, developers can automate various tasks to maintain code quality and consistency across their projects.
How to use husky?
First install husky and then add pre-commit
husky install
Now I have written a script that does everything for me
#!/bin/sh
# Check if the .husky directory exists
if [ ! -d ".husky" ]; then
echo "Creating .husky directory..."
mkdir .husky
fi
# Check if the pre-commit file exists
if [ ! -f ".husky/pre-commit" ]; then
rm -rf ./.husky/*
echo "Creating pre-commit file..."
touch .husky/pre-commit
echo '#!/bin/sh' > .husky/pre-commit
echo 'yarn build' >> .husky/pre-commit
chmod +x .husky/pre-commit
git config core.hooksPath .husky
fi
Make the above bash script executable and just run the above script after the husky install. This will setup a pre-commit hook where build will be executed before each commit. You can make use of any other command that need to be run before performing the commit.
What are some of the npm lifecycle scripts?
Lifecycle Scripts
Some of the key npm lifecycle scripts are:
prepare:
- Runs before the package is packed and published
- Runs on local
npm install
without any arguments - Runs after
prepublish
, but beforeprepublishOnly
prepublishOnly:
- Runs before the package is prepared and packed, only on
npm publish
prepack:
- Runs before a tarball is packed (on
npm pack
,npm publish
, and when installing a git dependency) - Note that
npm run pack
is not the same asnpm pack
postpack:
- Runs after the tarball has been generated and moved to its final destination
preinstall:
- Runs before the package is installed
postinstall:
- Runs after the package is installed
preuninstall:
- Runs before the package is uninstalled
postuninstall:
- Runs after the package is uninstalled
preversion:
- Runs before the version of the package is updated
postversion:
- Runs after the version of the package is updated
Some other notable lifecycle scripts includeprepublish
(deprecated),prepublishOnly
,prepack
, andpostpack
.