Skip to content

Instantly share code, notes, and snippets.

@gustavoalbuquerquebr
Last active January 15, 2021 10:53
Show Gist options
  • Save gustavoalbuquerquebr/80e42be2afcac90ad93c79e1d9e0951c to your computer and use it in GitHub Desktop.
Save gustavoalbuquerquebr/80e42be2afcac90ad93c79e1d9e0951c to your computer and use it in GitHub Desktop.
#yarn #cheatsheet

npm vs yarn

  • upon release, yarn had many advantages compared to npm
  • however, the rise of yarn pushed npm to make improvements and the competion has became increasingly tighten
  • though, yarn is still (even after npm 6.0) somewhat faster
  • the popularity gap is slowly closing but npm still is the most popular

Install

  • `sudo npm install -g yarn
  • curl --compressed -o- -L https://yarnpkg.com/install.sh | bash = update yarn

Basics

init

  • yarn init [-y]
  • yarn or yarn install = install all dependencies listed at 'package.json' and, if 'yarn.lock' exists and is enough to satisfy every dependency, the exact versions recorded in 'yarn.lock' are installed

info, list

  • yarn info package = info about a package = now you can use yarn as package manager anywhere, just like npm
    • yarn info package field = select a specific field, e.g. description, versions, license
  • yarn list = lists all dependencies for the current directory
    • yarn list --depth 0 --pattern "pattern"

install, upgrade, remove

  • yarn add [package]@[version] = install
    • yarn add [package] --dev|--peer|--optional = different categories of dependencies
  • yarn global add package = install globally
  • yarn outdated = checks for outdated package dependencies
  • yarn upgrade [package]@[version] = update dependency
  • yarn remove [package] = remove dependency

scripts

  • yarn run scriptname

cache

  • yarn stores every package in a global cache in your user directory on the file system
  • yarn cache list = view every cached package
    • yarn cache list --pattern "pattern"

yarn.lock

  • when you run either yarn or yarn add package, Yarn will generate a yarn.lock
  • this file will ensure that they get precisely the same dependencies as you have, is equivalent to npm's 'package-lock.json' and 'pm-shrinkwrap.json'

link

  • for development, a package can be linked into another project
  • two steps:
    • yarn link = in package you want to link
    • yarn link packagelinked = link the linked package into current directory

publish

  • yarn login
  • yarn publish, yarn publish --new-version <version>, yarn publish --access <public|restricted> = publish current package
  • as of now, no command to unpublish
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment