如何添加. npmrc 文件?

我在 Mac OS Sierra 上安装了节点。我在工作中使用 Windows,所以我有一个。Npmrc 文件放在 node 文件夹中,但我似乎在 mac 中找不到这个文件。 问题是我想添加一个格式的注册表

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

如何添加它,以便通过在 MAC OS Sierra 上运行 npm install 来安装项目的依赖项和模块。

我简单地创建了一个. npmrc 文件,并添加了上面的代码... 然后运行 npm install,就会得到以下错误

    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.
390489 次浏览

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

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

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

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