yarnpkgyarnpkg-v2.npmrc

.yarnrc private registry authentication with username and password


The instructions on how to migrate to Yarn 2 are unclear, and it's not obvious how to authenticate to a private registry which uses a username and password.

Here's the contents of my .npmrc file:

registry=https://pkgs.dev.azure.com/<private-path>/registry/
always-auth=true

//pkgs.dev.azure.com/<private-path>/registry/:username=${NPM_USER}
//pkgs.dev.azure.com/<private-path>/registry/:_password=${NPM_TOKEN}

The documentation for .yarnrc.yml only mentions how to deal with a token, but doesn't mention anything about a username. I've also tried using npmAuthIdent, but I don't know the correct syntax.

How can I authenticate with my private registry using yarn 2? For the moment I've had to downgrade back to yarn 1, so that I can use the npmrc file


Solution

  • What worked for me was to encode the username and password to base64, and then use the result string as the npmAuthIdent field :

    That means for instance :

    "myName:myPassword" to base64 => "bXlOYW1lOm15UGFzc3dvcmQ="

    So in you .yarnrc.yml you'll have :

    npmRegistryServer: "https://pkgs.dev.azure.com/<private-path>/registry/"
    npmAlwaysAuth: true
    npmAuthIdent: "bXlOYW1lOm15UGFzc3dvcmQ="
    

    You can convert myName:myPassword to base64 with:

    echo -n 'myName:myPassword' | base64