Skip to content

JavaScript

The goal of this document is to describe how to install the following tools:

  • Node and npm
  • 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.

Terminal window
sudo apt-get install build-essential

NodeJS

First install Node.js 20 (current) from the Node.js PPA.

Terminal window
# Download Nodesource GPG key
sudo apt-get update
sudo apt-get install -y ca-certificates curl gnupg
sudo mkdir -p /etc/apt/keyrings
curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | sudo gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg
# Create deb repo
NODE_MAJOR=20
echo "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.list
sudo apt-get update
sudo apt-get install nodejs -y

NVM

In case you need multiple Node.js versions, this could be the workflow.

Terminal window
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
source ~/.bashrc
nvm install v18
node -v
v18.7.1

Set a sane default in case the shipped NodeJS with the OS is old:

Terminal window
nvm alias default 20

Switching back to the system default:

Terminal window
nvm use system
node -v
v12.2.9

Other tools for managing versions of node: volta, fnm

npm

To install latest npm:

Terminal window
npm install -g npm@latest
npm -v
10.5.0

Configuration

After installing npm, it is highly recommended to add the following line to your .bashrc to enable bash completion:

Terminal window
. <(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.

Terminal window
corepack enable

pnpm

https://pnpm.io/

pnpm is a lot like npm, but handles dependencies in a different manner:

  1. 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.
  2. 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.
  3. Supports monorepos.

Just like yarn, all you have to do is enable corepack:

Terminal window
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:

Terminal window
brew install corepack

Useful commands

You don’t need run to use package.json scripts:

Terminal window
npm run <script>
# becomes
pnpm <script>

There’s two commands to run node packages:

Terminal window
# If the package is installed in the current project:
pnpm exec <package>
# If you don't have the package installed, equivalent to npx
pnpm dlx <package>

You can run pnpm as if you ran it in a different folder with:

Terminal window
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):

Terminal window
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:

Terminal window
# replace npm with projects package manager.
npm run prepare

This enables all hooks from the .husky folder.