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 20 (current) from the Node.js PPA.
# Download Nodesource GPG keysudo apt-get updatesudo apt-get install -y ca-certificates curl gnupgsudo mkdir -p /etc/apt/keyringscurl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | sudo gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg# Create deb repoNODE_MAJOR=20echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_${NODE_MAJOR}.x nodistro main" | sudo tee /etc/apt/sources.list.d/nodesource.listsudo apt-get updatesudo apt-get install nodejs -y
NVM
In case you need multiple Node.js versions, this could be the workflow.
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bashsource ~/.bashrcnvm install v18node -v v18.7.1
Set a sane default in case the shipped NodeJS with the OS is old:
nvm alias default 20
Switching back to the system default:
nvm use systemnode -v v12.2.9
Other tools for managing versions of node: volta, fnm
npm
To install latest npm:
npm install -g npm@latestnpm -v 10.5.0
Configuration
After installing npm, it is highly recommended to add the following line
to your .bashrc
to enable bash completion:
. <(npm completion)
Yarn
yarn has some advantages over npm (workspaces for mono-repositories,
mainly).
The official yarn docs
recommend enabling corepack
(shipped by default with Node.js) to access yarn.
corepack enable
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.