117

I installed node on my Mac OS Sierra. I use Windows at my work so there I have a .npmrc file in the node folder but I don't seem to find that in mac. The problem is I want to add a registry of the format

    "scope=rohit-project@rohit-aquila:registry=https://registry.npmjs.org/
    //registry.npmjs.org/:_authToken=some-token"

How do I add it so that I can install the dependencies and modules for my project by running npm install on MAC OS Sierra.

I created a .npmrc file simply and added the above code...and therefater running npm install I get the following error

    rohitsrivastava$ npm install
    npm ERR! Darwin 16.4.0
    npm ERR! argv "/usr/local/bin/node" "/usr/local/bin/npm" "install"
    npm ERR! node v7.7.3
    npm ERR! npm  v4.1.2
    npm ERR! code E404

    npm ERR! 404 Not found : @rohit-project/notes
    npm ERR! 404 
    npm ERR! 404  '@rohit-project/notes' is not in the npm registry.
    npm ERR! 404 You should bug the author to publish it (or use the name yourself!)
    npm ERR! 404 It was specified as a dependency of '@rohit-project/mega'
    npm ERR! 404 
    npm ERR! 404 Note that you can also install from a
    npm ERR! 404 tarball, folder, http url, or git url.
6
  • 2
    .npmrc is created when you npm login but you should be able to simply create a file at Users/rohit/.npmrc (username dependant) with that content. I hope that's not your real authToken..
    – lecstor
    Mar 19, 2017 at 11:49
  • call me paranoid, but I see no reason for you asking me to click a link to a google doc when you could add any extra information to your post so others could possibly assist as well. (gizmodo.com/…)
    – lecstor
    Mar 19, 2017 at 13:52
  • @lecstor Sorry for that.Just started using Stack Overflow yesterday. Mar 19, 2017 at 14:12
  • 8
    Is that your access token? Might want to go to npmjs.com/settings/binaryspike/tokens and delete it now it's public. Jun 22, 2019 at 21:51
  • 4
    pro tip, if you already have an .npmrc file, you can type npm config edit from the command line to open you npmrc file
    – Nicolasome
    Jul 27, 2022 at 19:43

6 Answers 6

183

There are a few different points here:

  1. Where is the .npmrc file created.
  2. How can you download private packages

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 (the field userconfig), as this is environment/operating system dependant. But if you have never logged in (using npm login) it will be empty. Simply log in to create it.

To just output the path prop, issue npm config get userconfig.

Another thing is #2 - downloading private packages. You can actually do that by putting a .npmrc file in the NPM package's root. It will then be used by NPM when authenticating. It also supports variable interpolation from your shell so you could do stuff like this:

; Get the auth token to use for fetching private packages from our private scope
; see http://blog.npmjs.org/post/118393368555/deploying-with-npm-private-modules
; and also https://docs.npmjs.com/files/npmrc
//registry.npmjs.org/:_authToken=${NPM_TOKEN}

Pointers

6
  • 3
    So npm knows how to read .npmrc lines like //registry.npmjs.org/:_authToken=${NPM_TOKEN}? That looks different from the other lines, which are key = value format. I'm just curious why that line is different. I see this blog post that shows it, but it doesn't explain how it works: blog.npmjs.org/post/118393368555/…
    – Carl G
    Feb 20, 2019 at 15:57
  • 5
    you can find the answer here: stackoverflow.com/a/53281483/2839274
    – Evan
    Apr 23, 2020 at 4:20
  • Heads up, I had to upgrade my version of NPM as the version I was on didn't allow for use of aliass
    – Jacksonkr
    Dec 15, 2020 at 15:58
  • @Jacksonkr Not sure why this is relevant. We do not use an alias commands here, AFAIK. How does your "alias" issue affect anything in this answer?
    – oligofren
    Dec 17, 2020 at 11:53
  • @oligofren My comment is not meant as a critique, more of a "if you find yourself using this answer as a potential solution and get stuck with an issue regarding aliases such as I did, here's a reminder check your NPM install for a potential update"
    – Jacksonkr
    Dec 17, 2020 at 18:14
23

In MacOS Catalina 10.15.5 the .npmrc file path can be found at

/Users/<user-name>/.npmrc

Open in it in (for first time users, create a new file) any editor and copy-paste your token. Save it.

You are ready to go.

Note: As mentioned by @oligofren, the command npm config ls -l will npm configurations. You will get the .npmrc file from config parameter userconfig

3
  • 1
    Upvoting this one because I'm too dumb to find the correct config property in the whole config on my own.
    – Carrm
    Oct 17, 2022 at 14:58
  • @Carrm Both @sunil and the accepted answer literally gives you this oneliner: npm config get userconfig. No need to find anything.
    – oligofren
    Feb 6 at 23:19
  • @oligofren Thanks but you updated your post with this information 5 days after I wrote my comment. At the time of the comment, only sunil posted the property to look for. Yours only got me a whole bunch of config parameters. So yeah, there was a need to go through the whole list of properties to find the correct one.
    – Carrm
    Feb 10 at 14:16
2

This issue is because of you having some local or private packages. For accessing those packages you have to create .npmrc file for this issue. Just refer the following link for your solution. https://nodesource.com/blog/configuring-your-npmrc-for-an-optimal-node-js-environment

2

Assuming you are using VSTS run vsts-npm-auth -config .npmrc to generate new .npmrc file with the auth token

2
  • 1
    It only works in Windows and non-bash environments
    – lvilasboas
    May 22, 2021 at 22:15
  • As of 2017 VSTS is deprecated. " The Azure CLI with the Azure DevOps extension has replaced the VSTS CLI. The VSTS CLI has been deprecated and will no longer be receiving new features. We recommend that users of the VSTS CLI switch to the Azure CLI and add the Azure DevOps extension." learn.microsoft.com/en-us/cli/vsts/…
    – oligofren
    Jul 7, 2021 at 8:59
1

In my case, updating my npm version helped me. So just to be sure, make sure your npm is up to date.

npm install -g npm@latest

0

In case if you want to have .npmrc file at project level

  1. In the root directory of your project, create .npmrc file.
  2. In the .npmrc file, add key=value
  3. Save and close the file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.