How to override npm project config? - node.js

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.

Related

Node.js will not install packages in project folder

I'm not sure what happened, but I've wasted six hours trying to fix Node.js as it just stopped working out of nowhere. Running Windows 11 with Powershell & Git Bash.
When I try to run npm install in my local git repository with a Next.js application (which I already initialized and even deleting and initializing again), it does not work; rather, it installs my project as a package WTFF??
Whenever I run create-next-app, it also installs the dependencies to the global folder.
I've tried:
Reinstalling Node.js (via Winget and Scoop)
Removing node_modules folder globally
Deleting package-lock.json
Creating a new project (doesn't work on any repository)
Configure --location flag at default startup to project, but it still did not fix it.
Deleting old package.json and initalizing a new one
Creating a new project
See below for screenshots.
What it says every time I run npm install in any repository
Result of npm list
Modified Configuration file
Please let me know if anyone has any idea, whether right or wrong. I tried looking online for related posts but couldn't find any like mine; if I overlooked any, please link them below. Thanks to anyone that helps, I have been losing my mind over this.
EDIT: When I set the flag for npm in the npm shim files, it does not work. But the workaround is to add --location=project every time I run an npm or npx command. I know you can set an alias to make life easier. How do I fix the configuration files?
I edited the shims located in: C:\Program Files\nodejs\node_modules\npm\bin and C:\Program Files\nodejs
I found out how to fix this problem; it had to do with a .npmrc already existing in my User folder, which automatically added it to the npm config, or I'm assuming. All I had to do with remove the .npmrc file in my home directory and run npm config set location=project, and it configured itself to install to the project directory by default.
Problem
.npmrc file existed when it should not have been there, so even though I reinstalled Node, the .npmrc never got deleted, so it always defaulted to the old configuration file and never worked.
Solution
Run npm config ls to see your configuration file
For me, there was a line in the configuration that looked like; "global" config from C:\Users\Ashwi\.npmrc
I deleted the file, then updated the configuration by setting the default location to project npm config set location=project
Then I restarted Powershell, and everything worked perfectly. However, to install global packages, you must add the --location=global flag every time you run the command.

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

Per-project npmrc :: How do I do this?

If this is my project layout: package.json.npmrc/client/server
and I want npm to install modules to: /server/node_modules
what should I set my prefix to?
Right now my .npmrc has: prefix = ./server but with this it just installs to directory anyway and puts an empty node_modules folder in the prefix destination. What am I doing wrong here?
Nevermind. I just read this link.
Edit: It simply can't be done yet, even though in their change log it says per-project npmrc support was available in v1.4.11.
It works in the CLI , so npm install package --prefix ./folder will put node_modules folder with package into prefix destination, which isn't very useful. Using --save with that will not update dependencies in package.json, either.

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