Package Managers
Before defaulting to NPM or Yarn, consider using PNPM.
NPM
NPM Commands
For more commands, see this cheatsheet.
NPM Initialization
npm init
- Create a new package.json filenpm i
- Can be used as a shortcut fornpm install
NPM Install
npm install
- Install all packagesnpm install <package>
- Install a packagenpm install <package> --save-dev
- Install a package as a dev dependencynpm install <package> --global
- Install a package globallynpm uninstall <package>
- Uninstall a package
NPM Update and Audit
npm update
- Update all packagesnpm outdated
- Check for outdated packagesnpm audit
- Check for vulnerabilities in installed packagesnpm audit fix
- Fix vulnerabilities in installed packagesnpm audit fix --force
- Fix vulnerabilities in installed packages and remove outdated packages
NPM Run
npm run <script>
- Run a script from the package.json file
Yarn
Yarn Commands
For more commands, see this cheatsheet.
Yarn Initialization
yarn init
- Create a new package.json file
Yarn Install
yarn
- Install all packages defined in package.json - same asnpm install
yarn add <package>
- Install a package - same asnpm install <package>
yarn add <package> --dev
- Install a package as a dev dependency
Yarn Upgrade
yarn upgrade
- Update all packages - same asnpm update
Yarn Package Updates
- Link to all documentation
yarn upgrade
- Upgrade all packages to their latest versionyarn upgrade left-pad
- Upgrade a specific package to its latest versionyarn upgrade left-pad@^1.0.0
- Upgrade a specific package to a specific versionyarn upgrade left-pad grunt
- Upgrade multiple packages to their latest versionyarn upgrade @angular
- Upgrade all packages with the name @angular to their latest versionyarn upgrade --latest
- Upgrade all packages to their latest versionyarn upgrade left-pad --latest
- Upgrade a specific package to its latest versionyarn upgrade left-pad@^1.0.0 --dev
- Upgrade a specific development dependency package to a specific versionyarn remove left-pad
- Removes a specific package
PNPM Commands
- Link to all documentation
- Introduction article
Switching Package Managers
From NPM to Yarn
>
- Install yarn using
npm i -g yarn
if not already installed - Go to the directory where you install packages and run the
yarn
command - Yarn will init and create its
yarn.lock
file - Now delete
package-lock.json
*Note
- In your
package.json
file replace all npm commands with yarn in "scripts" - Run
yarn dev
or whatever command you use for running a yarn script
*Note
: It is important you don't delete it before yarn command (as some people suggest) it can break things, for example your yarn command will not even work and it will throw error:Error: ENOENT: no such file or directory, open './package-lock.json'
References
From Yarn to PNPM
Migrating from yarn to pnpm is quite straightforward:
-
Install pnpm
npm install -g pnpm
if not already installed -
Rename all your yarn commands to pnpm:
yarn
->pnpm install
yarn test
->pnpm test
yarn package
->pnpm package
yarn deploy
->pnpm run deploy
(run is required here, aspnpm deploy
is a reserved command)
-
Replace all occurrences of the string
yarn.lock
in your source files withpnpm-lock.yaml
(search, prettier, etc.) -
In your CI/CD, when using
actions/setup-node@v3
, setcache
to'pnpm'
-
If you're using yarn PnP, remove
.yarnrc.yml
and the.yarn
folder -
In the root
package.json
set the packageManager key topnpm@<version>
(replace<version>
with the latest available version) -
Create a
pnpm-workspace.yaml
file with:packages: - 'services/*' - 'contracts/*' - 'packages/*'
and everything that is in the
workspaces
key of the rootpackage.json
-
Run
pnpm import
to generate apnpm-lock.yaml
from youryarn.lock
, then removeyarn.lock
-
Run
pnpm install
References