Using Yarn

JFrog Artifactory Documentation

ft:sourceType
Paligo
Overview

JFrog Artifactory supportsusing the Yarn client to easily work against Artifactory npm repositories. Yarn is a package manager that replaces the existing workflow for the npm client and is compatible with the npm registry. It contains the same feature set as the npm while operating faster, more securely, and more reliably.

Artifactory support of Yarn

Artifactory 7.x supports and has been tested with Yarn 1.22.4. Previous versions of Artifactory and Yarn have not been tested but should also work sufficiently.

Setting Up Yarn with Artifactory

For your Yarn package manager to work with Artifactory, you first need to authenticate Yarn with Artifactory by updating your npm config file, (i.e the.npmrcfile), with the credentials from Artifactory as follows:

  1. For Yarn to work with Artifactory, you first need to set the default npm registry with an Artifactory npm repository using the following command (the example below uses a repository called npm-repo ):

    npm config set registry https://artifactory.mycompany.com/artifactory/api/npm/<npm repository name>/
  2. Once you have set the default registry, you need to authenticate the npm client to Artifactory.

    npm config set always-auth true
  3. Run the following command in your npm client. When prompted, provide your Artifactory login credentials:

    npm login 

    At this point, the.npmrcfile will be updated and will display the following confirmation.

    registry=https://artifactory.mycompany.com/artifactory/api/npm/<npm repository name>/
    
    always-auth=true
    
    //artifactory.mycompany.com/artifactory/api/npm/<npm repository name>/
    
    :_authToken=ACCESS_TOKEN
    

    You can start using Yarn for installing and publishing packages.

Resolving npm Packages Using Yarn

To install npm packages and adding them as dependencies to your package.json, run the following Yarn command:

yarn add <PACKAGE_NAME>

An example

yarn add lodash

Alternatively, you can run the following command:

yarn add lodash --registry https://artifactory.mycompany.com/artifactory/api/npm/<npm repository name>/
Deploying npm Packages Using Yarn

To deploy your package to an Artifactory repository, you can do one of the following:

  • Add the following to thepackage.jsonfile:

    "publishConfig":{"registry":"https://artifactory.mycompany.com/artifactory/api/npm/<npm repository name>/"}

    And then run the default yarn publish command:

    yarn publish
  • Provide the npm repository you wish to publish to using the yarn publish command as follows:

    yarn publish --registry https://artifactory.mycompany.com/artifactory/api/npm/<npm repository name>/
Working with Scoped Packages

You can set up your project to use scoped packages by directing a scope name to your repository. Add the following line to your project’s .yarnrc to work with a scoped package. Replace @jfrog with the relevant scoped package name.

In this example, all Yarn download requests for @jfrog will be download from Artifactory.

For example:

@jfrog:registry “https://artifactory.mycompany.com/artifactory/api/npm/<npm repository name>/”
Resolving npm Scoped Packages

To resolve npm scoped packages, run the following command.

yarn add @<scope_name>/package

For example:

yarn add @jfrog/project-example