16

I am having issues with npm & the packages I have installed with npm. When I install the package "@date-io/core"using the npm package manager — everything works as expected, however; when I install the same package using yarn as the package manager an error occurs that prints the following message:

error couldn't find package "@date-io/core" on the "npm" registry.

I suspect that this is happening due to the scope hierarchy that npm uses for aquiring a configuration from an .npmrc file. I need to find out the location of the .npmrc file being used when I install the package, but I cannot figure out how to find out.

Does anyone know if it is possible to get the location of the .npmrc used by npm for any given npm command? For example, if I execute npm install some-package, no matter where I execute the command from, is there a way to know what .npmrc file npm used to configure itself?

JΛYDΞV
  • 8,532
  • 3
  • 51
  • 77
Jamie Hutber
  • 26,790
  • 46
  • 179
  • 291
  • 10
    Could try `npm config edit` at least, it opens the config file in an editor. Hopefully the you can get the path from the editor. You could also try increasing the CLI's verbosity, perhaps that'll tell you which registry is being used? `npm install --loglevel verbose` for npm, not sure what the flag is for yarn. – cbr Feb 02 '20 at 01:42
  • Thanks Cubrr, this does actually help me to see what is being loaded and thus work out which file it was. Unless I have 2 files with similar entries! – Jamie Hutber Feb 02 '20 at 02:01

2 Answers2

9

EDIT:

2022 - March 20th - 20:44 PST

       "This edit might not make a whole lot of sense if you have not yet read the original answer below."

This answer will not work for Node.js versions earlier than Node v14.0.0 (the solution works for: ^14.0.0). This was pointed out in the comments by @jonSakas who was insightful enough to know that the NPM CLI's publish command, when ran with the following flags, --dry-run --verbose, as shown here:

npm publish --dry-run --verbose

...does not print the locations of the systems .npmrc files.

END of EDIT



NPMRC File Locations: The What & Where

          NPM configuration files can, and will, exist at different levels in any given environment where Node projects are being developed. As far as I know, there are three specific levels, and they are as follows:

  1. BUILT-IN npmrc file
  2. GLOBAL npmrc file
  3. USER'S .npmrc file.



It Probably isn't Necessary to Know where the Global NPM Config File is.

         There are several was you can figure out where the .npmrc file you are using is at, but you don't need to. For any project you have, stick a .npmrc file in the root directory, right next to ${rootDir}/node_modules/ and ${rootDir}/package.json. The project-level .npmrc will override any other NPM files that are altering your project. I have never had NPM alter a project though, unless I changed a configuration file, which meant I knew about the file and where it was. If for some reason though, you still need to find a .npmrc, and changing the project level .npmrc document will not help you, you probably don't want to use the raw version of npm config edit, because that will just configure the project level npm document, and I think it will create one if there is not one, so its essentially does what at suggested at the beginning of the tangent I am on.

        Instead, throw a -g into the command, so you you open your global .npmrc configuration file instead, like this...

npm config -g edit

You it just dawned on me, you actually have another way you can see what configuration files are active. I think this way shows you the file-paths that npm looks at to see if there is an NPM file there or not.

Enter the following...

npm publish --dry-run --verbose

Don't worry if there's no package to publish, or if you have a package to publish, but its not ready to publish. I choose the publish dry-run command because its suppose to not make any changes. And when no package is present, it wont work, but it will still print the logging info you desire. When I did it, it printed the following.
jayd3v@jayd3v-XPS-8910:~$ npm publish --dry-run --verbose
npm verb cli [
npm verb cli   '/home/jayd3v/.nvm/versions/node/v17.6.0/bin/node',
npm verb cli   '/home/jayd3v/.nvm/versions/node/v17.6.0/bin/npm',
npm verb cli   'publish',
npm verb cli   '--dry-run',
npm verb cli   '--verbose'
npm verb cli ]
npm info using npm@8.5.2
npm info using node@v17.6.0
npm timing npm:load:whichnode Completed in 1ms
npm timing config:load:defaults Completed in 1ms
npm timing config:load:file:/home/jayd3v/.nvm/versions/node/v17.6.0/lib/node_modules/npm/npmrc Completed in 0ms
npm timing config:load:builtin Completed in 1ms
npm timing config:load:cli Completed in 1ms
npm timing config:load:env Completed in 0ms
npm timing config:load:project Completed in 2ms
npm timing config:load:file:/home/jayd3v/.npmrc Completed in 1ms
npm timing config:load:user Completed in 1ms
npm timing config:load:file:/home/jayd3v/.nvm/versions/node/v17.6.0/etc/npmrc Completed in 0ms
npm timing config:load:global Completed in 0ms
npm timing config:load:validate Completed in 0ms
npm timing config:load:credentials Completed in 0ms
npm timing config:load:setEnvs Completed in 1ms
npm timing config:load Completed in 8ms
npm timing npm:load:configload Completed in 8ms
npm timing npm:load:setTitle Completed in 0ms
npm timing config:load:flatten Completed in 2ms
npm timing npm:load:display Completed in 6ms
npm verb logfile /home/jayd3v/.npm/_logs/2022-03-01T22_15_38_103Z-debug-0.log
npm timing npm:load:logFile Completed in 3ms
npm timing npm:load:timers Completed in 0ms
npm timing npm:load:configScope Completed in 0ms
npm timing npm:load Completed in 19ms
npm verb publish [ '.' ]

You can see that it showed me the three areas that it tried to pull a configuration file ('.npmrc') from. It even named which was which



Built-in npmrc

npm timing config:load:file:/home/jayd3v/.nvm/versions/node/v17.6.0/lib/node_modules/npm/npmrc Completed in 0ms
npm timing config:load:builtin Completed in 1ms


User .npmrc

npm timing config:load:file:/home/jayd3v/.npmrc Completed in 1ms
npm timing config:load:user Completed in 1ms


Global npmrc

npm timing config:load:file:/home/jayd3v/.nvm/versions/node/v17.6.0/etc/npmrc Completed in 0ms
npm timing config:load:global Completed in 0ms



JΛYDΞV
  • 8,532
  • 3
  • 51
  • 77
2

As @cbr pointed out you can edit the config using this command:

npm config edit

also you can check these paths for npmrc per documentation: The four relevant files are:

  • per-project configuration file (/path/to/my/project/.npmrc)
  • per-user configuration file (defaults to $HOME/.npmrc; configurable via CLI option --userconfig or environment variable $NPM_CONFIG_USERCONFIG)
  • global configuration file (defaults to $PREFIX/etc/npmrc; configurable via CLI option --globalconfig or environment variable $NPM_CONFIG_GLOBALCONFIG)
  • npm's built-in configuration file (/path/to/npm/npmrc)

see here

sina.ce
  • 460
  • 2
  • 8