confused about .yarnrc and .npmrc - node.js

I have a private npm registry and a .npmrc file in the root directory of my project like this
_auth=${NPM_TOKEN}
always-auth=true
email=example#gmail.com
registry=https://example.com/artifactory/api/npm/
But I use yarn install to install the packages. Now, I am confused.
Should I create a .yarnrc file? If so, how can I convert .npmrc above to .yarnrc? Didn't find configurations such as _auth, always-auth in https://classic.yarnpkg.com/en/docs/yarnrc/
yarn installs packages from https://registry.yarnpkg.com, npm installs packages from https://registry.npmjs.org/. They have different registries. I am not sure yarn can installs packages from my private npm registry and obey the configurations of .npmrc.
I am going to use yarn. Should I build a private yarn registry?

I ran into the same issue. I just created .yarnrc file in the root folder of my project with the following content and it worked.
registry "<artifactory-url>"

Related

Installing a private package from Gitlab with Yarn

I have a private npm package that is published to the Gitlab Package Registry using a Gitlab CI pipeline.
I want to install this package in a project using yarn.
Following the documentation helped me come up with the following .npmrc file :
//gitlab.com/api/v4/packages/npm/:_authToken=glpat-***********
#my-org:registry=https://gitlab.com/api/v4/packages/npm/
With the above, npm install #my-org/my-package works perfectly.
However, yarn add #my-org/my-package fails. using --verbose shows a 404 :
verbose 1.169823875 Error: https://gitlab.com/api/v4/projects/<my-project-id>/packages/npm/#my-org/my-package/-/#my-org/my-package-1.0.3.tgz: Request failed "404 Not Found"
error An unexpected error occurred: "https://gitlab.com/api/v4/projects/<my-project-id>/packages/npm/#my-org/my-package/-/#my-org/my-package-1.0.3.tgz: Request failed \"404 Not Found\""
The package does exist at version 1.0.3, NPM installs it.
I need to get this to work with Yarn. How can I do this ?
Additionaly, is there a way to take the authToken out of the .npmrc file ?
I also was not able to install it with yarn, but npm was ok.
This this worked out:
npm config set -- //gitlab.com/api/v4/packages/npm/:_authToken=XXX
npm config set -- //gitlab.com/api/v4/projects/<projectID>/packages/npm/:_authToken=XXX
npm config set #my-scope:registry https://gitlab.com/api/v4/packages/npm/
yarn config set '//gitlab.com/api/v4/projects/:_authToken' "XXX"
yarn config set '//gitlab.com/api/v4/packages/npm/:_authToken' "XXX"
And no need for .npmrc for this setup.
Here is the official GitLab docs on that issue.
Your config in .npmrc should work if you are using Yarn v1.x, as Yarn 1 uses the registries configured in this file.
However, for Yarn 2, you must configure your private registries in the .yarnc.yml at the project level:
npmScopes:
my-org:
npmRegistryServer: "https://gitlab.com/api/v4/projects/my-project-id/packages/npm/"
npmAlwaysAuth: true
npmAuthToken: glpat-*******
Replace my-org with the scope of your package, my-project-id with the numeric id of your Gitlab project, and put your token with scope "api".

NPM default registry config to a private repo doesn't work

On my local MacOs, I have set registry npm config to https://<myrepo>.pkgs.visualstudio.com/_packaging/<myrepo>/npm/registry/ in both the project directory .npmrc beside package.json and also in $HOME\.nmprc.
But still, when I run npm i it doesn't replace https://registry.npmjs.org with the one configured above.
What else I am missing to config?
EDIT:
I realized that the problem is with dependency packages.
When I put .npmrc file in the same directory as package.json, running npm install will use the registry in npmrc.
You need to check if the package-lock.json file was generated during your previous npm install.
If yes, you could remove this file and run the npm install again.
Here is a doc about Package-lock.json. And the doc about configure the .npmrc on Mac/linux machine
On the other hand, you can also try to use the following command to set the registry:
npm config set registry feedurl

Install multiple npm packages from private gitlab registry

I'm using gitlab to host my private npm packages. At the moment I've 2 projects I published to the gitlab package registry. Both packages are used by another project (let's say 3rd project). According to the gitlab documentation, I installed both packages in the 3rd project using the following commands:
npm config set #myscope:registry https://gitlab.com/api/v4/projects/<first-project-id>/packages/npm/
npm config set "//gitlab.com/api/v4/projects/<first-project-id>/packages/npm/:_authToken" "<auth-token>"
npm install #myscope/first-package
npm config set #myscope:registry https://gitlab.com/api/v4/projects/<second-project-id>/packages/npm/
npm config set "//gitlab.com/api/v4/projects/<second-project-id>/packages/npm/:_authToken" "<auth-token>"
npm install #myscope/second-package
My problem is now that I cannot run a simple "npm install" anymore because my 3rd project depends on the 2 private packages. Sure, I can call "npm config set" before running "npm install" in the 3rd project, but the problem is that when I call "npm config set" for the second project, it replaces the first config (because both are having the same scope). So I can only install 1 package, for the second I'm getting an error because it cannot be found (because it has another project-id in gitlab, thus another package registry url). I already tried the following without success:
npm config set #myscope/first-package:registry https://gitlab.com/api/v4/projects/<first-project-id>/packages/npm/
npm config set "//gitlab.com/api/v4/projects/<first-project-id>/packages/npm/:_authToken" "<auth-token>"
npm install #myscope/first-package
npm config set #myscope/second-package:registry https://gitlab.com/api/v4/projects/<second-project-id>/packages/npm/
npm config set "//gitlab.com/api/v4/projects/<second-project-id>/packages/npm/:_authToken" "<auth-token>"
npm install #myscope/second-package
But now I cannot install any of the packages. So, anyone know how I can set the registry-url for packages sharing the same scope but having a different url?
I finally solved the issue by using the instance-level installation. First, I thought it's not working for me because it still told me 404. But the problem was that gitlab instructs me to run npm config set '//gitlab.com/api/v4/packages/npm/:_authToken' "<my-token>". But this is not working on my Windows because of the apostrophes that surrounds the url. I replaced them with " and now everything works fine.

Couldn't find package "XXX" on the "npm" registry

I am having problems with yarn install.
The package will install correctly with npm install but when installing with yarn the above error is produced. error Couldn't find package "google-map-react" on the "npm" registry.
.npmrc
#companyname:registry=https://npm.pkg.github.com/
//npm.pkg.github.com/:_authToken=476f....
I will admit, when removing the .npmrc file the error is still there. Which makes me think this must be some issue.
I also am making the assertion that the following command simply creates the .npmrc file in my home directory? npm login --registry=https://npm.pkg.github.com --scope=#companyname
I would ideally like to be able to install external NPM packages.
So I've found it after too long. I was seeing this error as yarn was using its config to connect to npm: npm.github.registry
I had previously worked in EuroStar which had their own private npm registry. Thus it was failing on simple packages because there npm is completely locked down.
So simple command:
yarn config delete registry
If you are using yarn, npmrc files are not being read no more:
Yarnrc files (named this way because they must be called .yarnrc.yml) are the one place where you'll be able to configure Yarn's internal settings. While Yarn will automatically find them in the parent directories, they should usually be kept at the root of your project (often your repository). Starting from the v2, they must be written in valid Yaml and have the right extension (simply calling your file .yarnrc won't do).
https://yarnpkg.com/configuration/yarnrc#npmRegistries
https://github.com/yarnpkg/yarn/issues/3612
https://yarnpkg.com/getting-started/migration
https://yarnpkg.com/configuration/yarnrc

How to override npm project config?

I have a nodejs project and placed .npmrc file at root to override default npm config. I need to change the npm project config while building the project. Is there any option available to override the npm project config?
I tried to run this
npm config set registry "https://registry.npmjs.org/"
but it couldn't override the npm project config.
To override use --registry
sample format
npm --registry registryurl install packagename
I realize this is very old. However, I believe that the project specific .npmrc file overwrites your global .npmrc file, not the global overwriting the project as you seemed to mention in your question.
From my research, it appears that there is no project-specific argument for the npm config set command. It only works on the global file, I think...
I found success by just running all the npm config set <key> <value> commands I needed, going to my global .npmrc file and copying all the new key/value pairs, pasting those into my project specific .npmrc file, then going back and deleting those new key/values from my global .npmrc file.

Resources