Connect Yarn to Artifactory

JFrog Artifactory Documentation

Products
JFrog Artifactory
Content Type
User Guide

You can use the Yarn client to install and publish npm packages. Artifactory supports multiple versions of Yarn:

Configure Yarn V2+ (Modern)

To connect Yarn Modern to Artifactory, you can use yarn commands to programmatically configure Yarn with the required permissions.

To connect Yarn Modern to Artifactory:

  1. Run this command to set Artifactory as your default npm registry:

    yarn config set --home npmRegistryServer "https://[JFrogPlatformURL]/api/artifactory/npm/<REPO_NAME>/"

    Where:

    • [JFrogPlatformURL]: The URL of your JFrog service

    • <REPO_NAME>: The name of the target repository

    For example:

    yarn config set --home npmRegistryServer "https://company.jfrog.io/api/artifactory/npm/npm-virtual/"
  2. Run this command to configure Yarn to always authenticate with Artifactory:

    yarn config set --home 'npmRegistries["https://[JFrogPlatformURL]/artifactory/api/npm/<REPO_NAME>/"].npmAlwaysAuth' true

    Where:

    • [JFrogPlatformURL]: The URL of your JFrog service

    • <REPO_NAME>: The name of the target repository

    For example:

    yarn config set --home 'npmRegistries["https://company.jfrog.io/artifactory/api/npm/npm-virtual/"].npmAlwaysAuth' true
  3. Run this command to set your authentication token for Artifactory:

    yarn config set --home 'npmRegistries["https://[JFrogPlatformURL]/artifactory/api/npm/<REPO_NAME>/"].npmAuthToken' <TOKEN>

    Where:

    • [JFrogPlatformURL]: The URL of your JFrog service

    • <REPO_NAME>: The name of the target repository

    • <TOKEN>: Your Artifactory identity token

    For example:

    yarn config set --home 'npmRegistries["https://company.jfrog.io/artifactory/api/npm/npm-virtual/"].npmAuthToken' Random_TokenPoYzI5MGRYSnZiM2w2SWpvaWRIaGlabWxmWTI5dEluMC5zVzE2ODU
  4. Optionally, enable non-secure HTTP interaction by running this command:

    yarn config set unsafeHttpWhitelist --home --json '["<HOSTNAME>"]'

    Where <HOSTNAME> is the hostname of your JFrog service, without the protocol. For example:

    yarn config set unsafeHttpWhitelist --home --json '["company.jfrog.io"]'

Configure Yarn V1 (Classic)

To use Yarn Classic with Artifactory, you need to configure the .npmrc file to connect to Artifactory. You can do this configuration in multiple ways:

Connect Yarn Classic to Artifactory using npm commands

To connect Yarn Classic to Artifactory:

  1. Run the following command to set an Artifactory repository as the default npm registry:

    npm config set registry [JFrogPlatformURL]/artifactory/api/npm/<REPO_NAME>/

    Where:

    • [JFrogPlatformURL]: The URL of your JPD

    • <REPO_NAME>: The name of the target repository

    For example:

    npm config set registry https://company.jfrog.io/artifactory/api/npm/npm-virtual/
  2. Run one of the following commands to authenticate to Artifactory, and enter your credentials when prompted:

    • For unscoped registries:

      npm login --auth-type=web
      echo "always-auth=true" >> ~/.npmrc
    • For scoped registries:

      npm login --auth-type=web --scope=@<SCOPE>
      echo "always-auth=true" >> ~/.npmrc

      Where <SCOPE> is the repository scope.

Note

You can also use JFrog Set me up to copy the snippet populated with your token and environment. For more information, see Use Artifactory Set Me Up for Configuring Package Manager Clients.

Connect Yarn Classic to Artifactory with Manual Configuration

If you can't log in manually or using automated scripts, or if you need to use a specific scope, you can edit your .npmrc file manually.

To manually configure the .npmrc file:

  1. Open the .npmrc file in a text editor. The location of the file varies by operating system:

    • Linux: ~/.npmrc

    • Mac: USER root folder

    • Windows: %USERPROFILE%/.npmrc

  2. Add one of the following snippets to the .npmrc file:

    • For unscoped registries:

      registry=https://[JFrogPlatformURL]/artifactory/api/npm/<REPO_NAME>/:_authToken=<TOKEN>

      Where:

      • [JFrogPlatformURL]: The URL of your JFrog service

      • <REPO_NAME>: The name of the target repository

      • <TOKEN>: Your Artifactory identity token

      For example:

      registry=https://company.jfrog.io/artifactory/api/npm/npm-local/:_authToken=Random_TokenPoYzI5MGRYSnZiM2w2SWpvaWRIaGlabWxmWTI5dEluMC5zVzE2ODU
    • For scoped registries:

      @<SCOPE>:registry=https://[JFrogPlatformURL]/artifactory/api/npm/<REPO_NAME>/:_authToken=<TOKEN>

      Where:

      • <SCOPE>: The scope of the repository

      • [JFrogPlatformURL]: The URL of your JFrog service

      • <REPO_NAME>: The name of the target repository

      • <TOKEN>: Your Artifactory identity token

      For example:

      @internal:registry=https://company.jfrog.io/artifactory/api/npm/npm-local/:_authToken=Random_TokenPoYzI5MGRYSnZiM2w2SWpvaWRIaGlabWxmWTI5dEluMC5zVzE2ODU
  3. Save the changes to the file.

Note

You can also use JFrog Set me up to copy the snippet populated with your token and environment. For more information, see Use Artifactory Set Me Up for Configuring Package Manager Clients.

Next steps: