Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I see which .npmrc file is being currently used

I am having very strange issues with my npm packages and installs.

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

However installing with npm produces no errors, but I need to install via yarn for many project settings and scripts.

So, is it possible to view which .npmrc is being currently used in my project?

like image 482
Jamie Hutber Avatar asked Feb 02 '20 01:02

Jamie Hutber


People also ask

What is the npmrc file?

The .npmrc file. npm gets its config settings from the command line, environment variables, and npmrc files. The npm config command can be used to update and edit the contents of the user and global npmrc files. For a list of available configuration options, see npm-config(7). Files. The four relevant files are:

How to check if npmrc is empty?

Running npm config ls -l will show you all the implicit settings for npm, including what it thinks is the right place to put the .npmrc, as this is environment/operating system dependant. But if you have never logged in (using npm login) it will be empty.

Where can I find NPM config files?

per-project config file (/path/to/my/project/.npmrc) All npm config files are an ini-formatted list of key = value parameters.

How do I change the environment variables in npmrc?

per-project config file (/path/to/my/project/.npmrc) All npm config files are an ini-formatted list of key = value parameters. Environment variables can be replaced using $ {VARIABLE_NAME}. For example: Each of these files is loaded, and config options are resolved in priority order.


1 Answers


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 [email protected]
npm info using [email protected]
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



like image 132
j D3V Avatar answered Sep 19 '22 05:09

j D3V