JavaScript
The goal of this document is to describe how to install the following tools:
Node
andnpm
yarn
pnpm
Husky
It is not necessary to follow this document exactly, as long as above tools can be used from the command line.
Prerequisites
Some Node.js libraries depend on
node-gyp for installation,
which requires make
and a C/C++
compiler.
sudo apt-get install build-essential
NodeJS
First install Node.js 22 (current LTS) from the Node.js PPA.
sudo apt-get install -y curlcurl -fsSL https://deb.nodesource.com/setup_22.x | sudo -E bash -sudo apt-get install -y nodejsnode -v
NVM
In case you need multiple Node.js versions, this could be the workflow.
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.3/install.sh | bash\. "$HOME/.nvm/nvm.sh"nvm install 22node -v
Should echo something like v22.17.1
.
Set a sane default in case the shipped NodeJS with the OS is old:
nvm alias default 22nvm use default
Switching back to the system default:
nvm use system
Should echo something like v18.20.4
.
npm
To install latest npm:
npm install -g npm@latestnpm -v
Should echo something like 11.5.1
.
Configuration
After installing npm, it is highly recommended to add the following line
to your .bashrc
to enable bash completion:
. <(npm completion)
pnpm
pnpm
is a lot like npm
, but handles dependencies in a different manner:
- It stores all packages in a central cache and uses hard links to add them to
node_modules
folders. This means every package is present on your computer only once, saving a lot of disk space. It also has significantly faster installs due to just creating hard links. - It doesn’t flatten your dependencies. Because of the hard links it can add peer dependencies to the package that depends on them, instead of to the top level. This means you can’t access packages you don’t directly depend on from your code.
- Supports monorepos.
Just like yarn
, all you have to do is enable corepack
:
corepack enable
Once you’ve done that you can use pnpm
commands, and corepack
will automatically install the version of pnpm
the
project’s package.json
has listed, or the most recent pnpm
version if there isn’t one set.
Brew: if you have node installed via homebrew, you need to install corepack first:
brew install corepack
Useful commands
You don’t need run
to use package.json
scripts:
npm run <script># becomespnpm <script>
There’s two commands to run node packages:
# If the package is installed in the current project:pnpm exec <package># If you don't have the package installed, equivalent to npxpnpm dlx <package>
You can run pnpm
as if you ran it in a different folder with:
pnpm -C <path> <command>
# Run the frontend dev server from the monorepo's folder:pnpm -C frontend dev
Every now and then, you might want to clean up unreferenced packages from your store (e.g. packages that aren’t hard linked to any project):
pnpm store prune
Husky
https://typicode.github.io/husky/
Husky is a npm package that manages git hooks for the repo.
Assuming the project has husky initialized (the .husky
folder exists) you can just run the prepare
script:
# replace npm with projects package manager.npm run prepare
This enables all hooks from the .husky
folder.