Ignore local .npmrc config file during npm install - node.js

How do I ignore a local .npmrc config file while running npm install?
Is there a command like npm install --ignore-local-rc or something similar? I feel this can be useful for situations where you want to pull packages from the public npm registry by-passing the private registries specified.

You can use --userconfig or --globalconfig to bypass the local user config file or global config file respectively. I don't think there is a way to bypass a project .npmrc with a command-line flag. Using --userconfig=/dev/null should bypass the userconfig (on Linux and other UNIX-like operating systems).
For situations where you need to bypass only the registry setting, you can use the --reg or --registry command line flags, or set an npm_config_registry environment variable.

Related

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

How to specify registry while doing npm install with git remote url?

I want to be able to clone a git repository using a URL as specified here
<protocol>://[<user>[:<password>]#]<hostname>[:<port>][:][/]<path>[#<commit-ish>]
I am getting an error saying
npm ERR! 404 Registry returned 404 for GET on
https://registry.npmjs.org/XYZ
So I should also be able to specify the registry while doing since modules are supposed to be picked up from a internal repository.
Is it possible to specify registry while doing npm install with git remote url?
npm gets its config settings from the command line, environment variables, and npmrc files. You can try to specify registry in a npmrc file, and module in the command line.
To change registry, you can use command:
npm config set registry <registry url>
You can also change configs with the help of -- argument. Putting --foo bar on the command line sets the foo configuration parameter to "bar". So you can try something like that:
npm install http://git.repo.url --registry=https://your.registry.local/
Not the best way but If you are using mac or linux even in you can set alias for different registries.
##############NPM ALIASES######################
alias npm-default='npm config set registry https://registry.npmjs.org'
alias npm-sinopia='npm config set registry http://localhost:4873/'
Yes, you need to use:
npm config set registry <registry url>.
to make sure you install your package from the registry inside your company.
If you are doing npm -i -g, this is to install globally.
you need to do:
npm -g config set registry <registry url>

Is there any way to configure multiple registries in a single npmrc file

Here is my problem. We have a private NPM registry which only works in VPN. I would like to have a fallback registry https://registry.npmjs.org so that when I am out of VPN it works seamlessly.
P.S. Currently I am using npmrc which does a good job in switching between .npmrc files as a workaround
You can have multiple registries for scoped packages in your .npmrc file. For example:
#polymer:registry=<url register A>
registry=http://localhost:4873/
Packages under #polymer scope will be received from https://registry.npmjs.org, but the rest will be received from your local NPM.
On version 4.4.1, if you can change package name, use:
npm config set #myco:registry http://reg.example.com
Where #myco is your package scope.
You can install package in this way:
npm install #myco/my-package
For more info: https://docs.npmjs.com/misc/scope
I believe the top-voted answer might be outdated. As of June 2021, there is a much easier way to do this using npmrc.
Refer to npm Docs.
1. Install npmrc
To install npmrc, on the command line, run
npm i npmrc -g
2. Create your first npm profile
After installing npmrc, you can create a profile to access your custom (maybe company's) registry.
To create an npm Enterprise profile, on the command line, run npmrc -c name-of-profile.
For example, to create a profile called "work", run the following command:
npmrc -c work
To set an npm Enterprise registry for the profile, run the following command, replacing your-company-registry with the name of your company's npm Enterprise registry:
npm config set registry https://registry.your-company-registry.npme.io/
3. Create a profile for the public npm registry
After you have created your npm Enterprise profile, you can create a second profile for a different registry, such as the public npm registry.
To create a profile for the public registry, on the command line, run npmrc -c name-of-profile. For example, to create a profile called "open-source", run npmrc -c open-source.
To set the public registry for your open source profile, run the following command:
npm config set registry https://registry.npmjs.org/
4. Switch profiles with npmrc
To switch profiles, on the command line, run the following command, replacing profile-name with the name of your profile:
npmrc profile-name
Not the best way but If you are using mac or linux even in windows you can set alias for different registries.
##############NPM ALIASES######################
alias npm-default='npm config set registry https://registry.npmjs.org'
alias npm-sinopia='npm config set registry http://localhost:4873/'
For anyone looking also for a solution for authentication, I would add on the scoped packages solution that you can have multiple lines in your .npmrc file:
//internal-npm.example.com:8080/:_authToken=xxxxxxxxxxxxxxx
//registry.npmjs.org/:_authToken=yyyyyyyyyy
Each line represents a different NPM registry
Since it has been a couple years and it doesn't seem possible to do this (using npm alone), a solution to this problem is to use the Nexus Repository Manager (from Sonatype). Nexus supports multiple repositories, lets you order them, and also proxies/caches to improve speed.
A free version and pro/paid version exist. The feature that supports this is described at:
https://help.sonatype.com/repomanager3/node-packaged-modules-and-npm-registries
The relevant information is duplicated below so if/when the above URL/link stops working the information is still here.
A repository group is the recommended way to expose all your npm registries repositories from the repository manager to your users, without needing any further client side configuration. A repository group allows you to expose the aggregated content of multiple proxy and hosted repositories with one URL to npm and other tools.
It lets you create private npm registries
A private npm registry can be used to upload your own packages as well as third-party packages.
And
To reduce duplicate downloads and improve download speeds for your developers and CI servers, you should proxy the registry hosted at https://registry.npmjs.org. By default npm accesses this registry directly. You can also proxy any other registries you require.
So a quick bulleted list of things you do to get this working is:
Install Nexus
Create a local/private repo (or point to your private repo on another server)
Create a GROUP that lists your private repo, and the public repo.
Configure your $HOME/.npmrc file to point to the "GROUP" just created.
Publish your private npm packages to the local repo.
Users now can run a one time setup.
npm config set registry https://nexus/content/groups/GROUP
Then users can install both public or private packages via npm install.
npm install my-private-package
npm install lodash any-other-public-package
And both your public and private packages can be installed via a simple npm install command. Nexus finds the package searching each repo configured in the group and returns the results. So npm still thinks there is just one registry but behind the curtain there are multiple repos being used.
IMPORTANT NOTE: When you publish your components, you'll need to specify the npm publish --registry https://nexus/content/repositories/private-repo my-private-package command so your package is published to the correct repo.
You can use multiple repositories syntax for the registry entry in your .npmrc file:
registry=http://serverA.url/repository-uri/
//serverB.url/repository-uri/
//serverC.url/repository-uri/:_authToken=00000000-0000-0000-0000-0000000000000
//registry.npmjs.org/
That would make your npm look for packages in different servers.
Some steps you can try. (its how we do it at my workplace)
Create a registry group with two (or more) repository source address. One would be your internal private and the other a proxy to npmjs giving priority to the internal one.
Make this group your registry in the .npmrc file. This way npm will always try to get it from the internal one, if not found get it from the proxy
Hope that helps.
I encounter the same problem when my company set up its own registry, so I heavily rework on proxy-registry into proxy-multi-registries to solve this problem. Hope it will also helps you.
As of 13 April 2020 there is no such functionality unless you are able to use different scopes, but you may use the postinstall script as a workaround. It is always executed, well, after each npm install:
Say you have your .npmrc configured to install #foo-org/foo-pack-private from your private github repo, but the #foo-org/foo-pack-public public package is on npm (under the same scope: foo-org).
Your postinstall might look like this:
"scripts": {
...
"postinstall": "mv .npmrc .npmrcc && npm i #foo-org/foo-pack --dry-run && mv .npmrcc .npmrc".
}
Don't forget to remove #foo-pack/foo-org from the dependencies array to make sure npm install does not try and get it from github and to add the --dry-run flag that makes sure package.json and package-lock.json stay unchanged after npm install.
My approach was to make a slight command line variant that adds the registry switch.
I created these files in the nodejs folder where the npm executable is found:
npm-.cmd:
#ECHO OFF
npm --registry https://registry.npmjs.org %*
npm-:
#!/bin/sh
"npm" --registry https://registry.npmjs.org "$#"
Now, if I want to do an operation against the normal npm registry (while I am not connected to the VPN), I just type npm- where I would usually type npm.
To test this command and see the registry for a package, use this example:
npm- view lodash
PS. I am in windows and have tested this in Bash, CMD, and Powershell. I also
I use Strongloop's cli tools for that; see https://strongloop.com/strongblog/switch-between-configure-public-and-private-npm-registry/ for more information
Switching between repositories is as easy as : slc registry use <name>
I had the same issue and I've tried many solutions that didn't work, now i encountered that by using different scoops for my npm private packages, in that way in can use multiple registries with same .npmrc file like that :
.npmrc : (You can put as many packages you want)
#scop1:registry=https://gitlab.example.com/api/v4/projects/<project1_id>/packages/npm/
#scop2:registry=https://gitlab.example.com/api/v4/projects/<project2_id>/packages/npm/
//gitlab.example.com/api/v4/projects/<project1_id>/packages/npm/:_authToken=${GITLAB_TOKEN}
//gitlab.example.com/api/v4/projects/<project2_id>/packages/npm/:_authToken=${GITLAB_TOKEN}
No, NPM does not support multiple registry except for scoped one.
Refer to: https://docs.npmjs.com/cli/v7/using-npm/scope

How to restore/reset npm configuration to default values?

I have played with npm set and npm config set for several times, now I want to reset to default values (a kind of factory reset).
Does npm provide a command to do that? Or should I delete all configuration files by hands then reinstall it?
I need to do it both on Linux CentOS and on Windows 8.
To reset user defaults
Run this in the command line (or git bash on windows):
echo "" > $(npm config get userconfig)
npm config edit
To reset global defaults
echo "" > $(npm config get globalconfig)
npm config --global edit
If you need sudo then run this instead:
sudo sh -c 'echo "" > $(npm config get globalconfig)'
For what it's worth, you can reset to default the value of a config entry with npm config delete <key> (or npm config rm <key>, but the usage of npm config rm is not mentioned in npm help config).
Example:
# set registry value
npm config set registry "https://skimdb.npmjs.com/registry"
# revert change back to default
npm config delete registry
If you run npm config edit, you'll get an editor showing the current configuration, and also a list of options and their default values.
But I don't think there's a 'reset' command.
If it's about just one property - let's say you want to temporarily change some default, for instance disable CA checking: you can do it with
npm config set ca ""
To come back to the defaults for that setting, simply
npm config delete ca
To verify, use npm config get ca.
npm config edit
Opens the config file in an editor. Use the --global flag to edit the global config.
now you can delete what ever the registry's you don't want and save file.
npm config list will display the list of available now.
Config is written to .npmrc files so just delete it. NPM looks up config in this order, setting in the next overwrites the previous one. So make sure there might be global config that usually is overwritten in per-project that becomes active after you have deleted the per-project config file. npm config list will allways list the active config.
npm builtin config file (/path/to/npm/npmrc)
global config file ($PREFIX/etc/npmrc)
per-user config file ($HOME/.npmrc)
per-project config file (/path/to/my/project/.npmrc)

How can I change the cache path for npm (or completely disable the cache) on Windows?

I've installed Node.js on my Windows 7 x64 development machine, the manual way:
mkdir C:\Devel\nodejs
cd C:\Devel\nodejs
set NODE_PATH=%CD%
setx /M PATH "%PATH%;%NODE_PATH%"
setx /M NODE_PATH "%NODE_PATH%\node_modules"
I've placed the main node x64 binary along with npm package manager in C:\Devel\nodejs. Works like a charm and I can update the main binary without dealing with the installer.
The only problem I can't solve is moving the cache folder. When I install a local package:
npm install express
... cache is placed under %APP_DATA%\npm-cache folder. I'd like to change it to:
C:\Devel\nodejs\npm-cache
How can I change the npm cache folder, or disable it completely?
You can change npm cache folder using the npm command line. (see https://docs.npmjs.com/cli/v6/using-npm/config#cache)
So you might want to try this command :
> npm config set cache C:\Devel\nodejs\npm-cache --global
Then, run npm --global cache verify after running this command.
You can also set an environment variable with export npm_config_cache=/path/to/cache (Unix) or set npm_config_cache=C:\path\to\cache (Win) as an alternative to npm config set (this is true for all config options in npm).
For anyone using docker you can add the env var at runtime with:
docker run -e npm_config_cache=/path/to/cache mydockerimage:tag
You can also do following:
For having cache path as you wish, for a single package while installing it:
npm install packageName --cache path/to/some/folder
For having cache path as you wish, for all the packages in package.json:
Just be in the directory where package.json is as usual and do
npm install --cache path/to/some/folder
You may not find this in npm documentation but i have tried it with npm 6 and it works.
Looks like it works since npm 5 [Refer: How to specify cache folder in npm5 on install command?
In Windows you can simply cd to the desired cache folder and do npm set cache --global
Solution
Paste the following code into npmrc file.
Location of npmrc file: C:\Program Files\nodejs\node_modules\npm\npmrc
prefix=D:\nodejs\npm
cache=D:\nodejs\npm-cache
Notes:
There is no '.' in front of npmrc
Diagrams
NPMRC file folder look like this
NPMRC Content look like this
Hope it helps. Cheers
In addition, I found that running an update command works also - for example:
npm update npm
Lastly, one can check their npm-cache directory to see if is being filled or not.

Resources