238

When I use npm install fancyapps/fancybox#v2.6.1 --save, so fancybox package at v2.6.1 tag will be installed. This behavior is described in docs

I want to ask, how to do this with yarn?

Is this command the right alternative? In yarn docs isn't anything about this format.

yarn add fancyapps/fancybox#v2.6.1
Penny Liu
  • 15,447
  • 5
  • 79
  • 98
Silver Zachara
  • 2,901
  • 2
  • 16
  • 22

7 Answers7

347

You can add any Git repository (or tarball) as a dependency to yarn by specifying the remote URL (either HTTPS or SSH):

yarn add <git remote url> installs a package from a remote git repository.
yarn add <git remote url>#<branch/commit/tag> installs a package from a remote git repository at specific git branch, git commit or git tag.
yarn add https://my-project.org/package.tgz installs a package from a remote gzipped tarball.

Here are some examples:

yarn add https://github.com/fancyapps/fancybox [remote url]
yarn add ssh://github.com/fancyapps/fancybox#3.0  [branch]
yarn add https://github.com/fancyapps/fancybox#5cda5b529ce3fb6c167a55d42ee5a316e921d95f [commit]

(Note: Fancybox v2.6.1 isn't available in the Git version.)

To support both npm and yarn, you can use the git+url syntax:

git+https://github.com/owner/package.git#commithashortagorbranch
git+ssh://github.com/owner/package.git#commithashortagorbranch
Community
  • 1
  • 1
Kasiriveni
  • 5,671
  • 1
  • 22
  • 31
49

Yarn 2+

Installing from remote URLs has changed slightly with Yarn 2. Specifically, remote URLs must be prefixed with the package name. So for github this means:

yarn add '<package name>@https://github.com/<github user>/<github repo>'

Make sure that <package name> matches the value in the "name" field of the repo's package.json file.

To target a specific branch add either head=<branch> or commit=<full commit hash> via the URL fragment:

yarn add '<package name>@https://github.com/<github user>/<github repo>#head=<branch name>'

If you're trying to install an individual package from a Yarn monorepo on github you can add workspace=<package name> to the URL fragment:

yarn add '<package name>@https://github.com/<github user>/<github repo>#head=<branch name>&workspace=<package name>'
Ray
  • 2,713
  • 3
  • 29
  • 61
Elliot
  • 1,463
  • 1
  • 14
  • 14
34

For ssh style urls just add ssh before the url:

yarn add ssh://<whatever>@<xxx>#<branch,tag,commit>
Tyler
  • 1,705
  • 2
  • 18
  • 26
  • 10
    This worked for me when `yarn` refused to honor the common `git` ssh url syntax: Did work: `yarn add ssh://git@github.com:my-org/my-repo#commit_hash` Did not work: `yarn add git@github.com:my-org/my-repo#commit_hash` – kilogic Oct 01 '18 at 16:19
  • 1
    Is there a way to do that but from package.json with yarn install? – Kévin Sanchez Lacroix Aug 13 '20 at 13:05
21

This is described here: https://yarnpkg.com/en/docs/cli/add#toc-adding-dependencies

For example:

yarn add https://github.com/novnc/noVNC.git#0613d18
lanwen
  • 2,251
  • 1
  • 17
  • 30
15

For GitHub (or similar) private repository:

yarn add 'ssh://git@github.com:myproject.git#<branch,tag,commit>'
npm install 'ssh://git@github.com:myproject.git#<branch,tag,commit>'
Eduardo Cuomo
  • 17,828
  • 6
  • 117
  • 94
11

I use this short format for github repositories:

yarn add github_user/repository_name#commit_hash

betoharres
  • 1,736
  • 2
  • 19
  • 25
2

With the latest version of Yarn, they require a package name before the URL. If you're installing a private package that you published to Github Packages, the syntax is like this:

yarn add @organization/packagename@https://github.com/organization/packagename,

Which should yield this line in package.json:

"@organization/packagename": "https://github.com/organization/packagename"