Skip to content

Instantly share code, notes, and snippets.

@yokawasa
Last active March 14, 2024 06:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yokawasa/de92cecdc420088a0d29903199448a0b to your computer and use it in GitHub Desktop.
Save yokawasa/de92cecdc420088a0d29903199448a0b to your computer and use it in GitHub Desktop.
Node.js package manager cheat sheet

Node.js package managers cheat sheet

Yarn and npm

Overview

features npm yarn
lock file package-lock.json yarn.lock
using workspaces supported supported
remote scripts supported using the npx supported using yarn dlx
license check not supported check each package license while downloading

ref: https://phoenixnap.com/kb/yarn-vs-npm

As described in yarn-vs-npm, Yarn seems to have huge advantages over npm:

  • Yarn Advantages
    • Supports parallel installation and Zero installs, both of which dramatically increase performance.
    • Newer versions of Yarn offer a more secure form of version locking.
    • Active user community.
  • Yarn Disadvantages
    • Yarn doesn't work with Node.js versions older than version 5.
    • Yarn has shown problems when trying to install native modules.

Also, here is another "Yarn better than NPM" description

Yarn installs dependency packages in parallel, whereas NPM installs them sequentially. As a result, Yarn outperforms NPM when installing bigger files. Both tools can save dependent files to the offline cache

ref: https://www.imaginarycloud.com/blog/npm-vs-yarn-which-is-better/

Commands

Yarn

yarn installation

npm install -g yarn

commands

# Initialize a project
yarn init

# Run tests for the current package
yarn test

# Check for outdated packages
yarn outdated

# Publish a package
yarn publish

# Run a script
yarn run

# Manage local package cache
yarn cache clean

# Log in or out
yarn login/logout # use `npm login` instead

# Install dependencies
yarn

# Install packages
# install package as a dependent one (you need this package to run) 
yarn add [package name]
# install package as a dev dependency? (you need it such as test suites but you don't need it (test suites ) to run the app in its normal state) 
yarn install [package name] --dev

# Uninstall packages
yarn remove [package name]

# Update manager
yarn upgrade

# Update packages
yarn upgrade [package name]

# Install packages globally
yarn global add [package name]

# Uninstall packages globally
yarn global remove [package name]

# Interactive dependency update	
yarn upgrade-interactive

# Run package remotely
yarn dlx

# Check licenses
yarn licenses ls

# run Jest
yarn jest

npm

# Initialize a project
npm init

# Run tests for the current package
npm test

# Check for outdated packages
npm outdated

# Publish a package
npm publish

# Run a script
npm run

# Manage local package cache
npm cache clean

# Log in or out
npm login/logout 
npm login --scope=@my-company-name
npm login --scope=@my-company-name --registry=http://your-server-address:8080
# ref: https://stackoverflow.com/questions/38211512/login-to-npm-with-multiple-scopes

# Install dependencies
npm install

# Install packages
npm install [package name]
## HTTPS
npm install git+https://account_name/repository_name.git
## SSH
npm install git+ssh://git@github.com:account_name/repository_name.git
## Local
npm install ../local_package_name

# install package as a dependent one (you need this package to run) 
npm install [package name] --save
# install package as a dev dependency? (you need it such as test suites but you don't need it (test suites ) to run the app in its normal state) 
npm install [package name] --save-dev

# Uninstall packages
npm uninstall [package name]

# Update manager
npm update

# Update packages
npm update [package name]

# Install packages globally
npm install --global [package name]

# Uninstall packages globally
npm uninstall --global [package name]

# Interactive dependency update	
npm run upgrade-interactive

REFERENCES

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment