npm config - global vs local

68.8k Views Asked by At

Here are the relevant docs on the subject:

https://docs.npmjs.com/cli/config

It looks like these are equivalent:

npm config set foo bar
npm set -g foo bar

so I figured that if I run npm set without the -g switch, then it would update my local .npmrc file:

npm set foo bar

but npm set w/o the -g flag did not update my local .npmrc file. So what the hell is the difference between npm set and npm config set, and what is the -g flag for?

2

There are 2 best solutions below

2
On

There are 4 npmrc files used by npm (ref: https://docs.npmjs.com/files/npmrc.html).

  1. per-project config file (/path/to/my/project/.npmrc)
  2. per-user config file (~/.npmrc)
  3. global config file ($PREFIX/etc/npmrc)
  4. npm builtin config file (/path/to/npm/npmrc)

They are modified this way:

  1. There does not seem to be a standard way (a command) for setting values to the per-project config file. I think you have to edit with a text editor.

  2. You use the npm [config] set command to set per-user values.

  3. You use the npm [config] set -g command to set global values.

  4. You don't need to edit the build in version

Use the first method in your case.

Please note that each of these files is loaded, and config options are resolved in priority order. For example, a setting in the userconfig file would override the setting in the globalconfig file.

0
On

npm create two .npmrc files.

  1. one is local(specific to per user)
  2. and one global which store configuration for all users.

npm config command is used to update and retrive configurations in/from .npmrc file.

  • when we use -g flag changes are made to global .npmrc file.
  • if -g is not present then changes made to local files.

npm config set and npm set both commands do the same thing.