NPM 配置文件修改

NPM 配置文件修改

几乎每一门语言都有配套的包管理器,比如 Ruby 有 RubyGems,Go 有 go modules,npm 作为 node 的包管理器,你有想过全局安装的 node 包都放在硬盘里面的哪个地方?它有没有配置文件?node 包的缓存位置在哪里?


node 的包管理工具是 npm,npm 的 配置文件是 .npmrc 文件,您可以通过 npm -h 来查看 npm 所支持的相关命令:

npm -h

// 输出结果(在我个人的Mac系统上)

Usage: npm <command>

where <command> is one of:
    access, adduser, audit, bin, bugs, c, cache, ci, cit,
    clean-install, clean-install-test, completion, config,
    create, ddp, dedupe, deprecate, dist-tag, docs, doctor,
    edit, explore, get, help, help-search, hook, i, init,
    install, install-ci-test, install-test, it, link, list, ln,
    login, logout, ls, org, outdated, owner, pack, ping, prefix,
    profile, prune, publish, rb, rebuild, repo, restart, root,
    run, run-script, s, se, search, set, shrinkwrap, star,
    stars, start, stop, t, team, test, token, tst, un,
    uninstall, unpublish, unstar, up, update, v, version, view,
    whoami

npm <command> -h  quick help on <command>
npm -l            display full usage info
npm help <term>   search for help on <term>
npm help npm      involved overview

Specify configs in the ini-formatted file:
    /Users/arraybuffer/.npmrc # 配置文件路径在这里
or on the command line via: npm <command> --key value
Config info can be viewed via: npm help config

npm@6.11.3 /usr/local/lib/node_modules/npm # npm 可执行文件所在地址

上图显示当前我的 npm 版本为 6.11.3 ,配置文件所在位置为 /Users/arraybuffer/.npmrc 

快捷修改 .npmrc 配置文件

.npmrc 文件就是 npm 的配置文件所在位置,寻找这个文件的目的,多数是为了修改 .npmrc 文件内容。但npm 提供了方便快捷的修改方式,尽管不知道这个文件的位置,其实也是可以修改的:

npm config edit

image.png


在弹出的界面上,修改 .npmrc 文件后,保存即可。

npm config list 简要信息获取

终端输入:

npm confi list

输出结果:

; cli configs
metrics-registry = "https://registry.npmjs.org/" // npm 包源
scope = ""
user-agent = "npm/6.11.3 node/v10.15.0 darwin x64"

; builtin config undefined
prefix = "/usr/local" // 全局配置的前缀

; node bin location = /usr/local/bin/node
; cwd = /usr/local/lib/node_modules
; HOME = /Users/arraybuffer
; "npm config ls -l" to show all defaults.

.npmrc 文件内容详情

可以通过 npm config ls -l 列出所有配置信息,这些信息以 key/value 的形式存储和展示。

  • npm config get [keyName] 该命令可以获取值
  • npm config set [keyName] [keyValue]  改命令可以设置值
;;;;
; npm userconfig file
; this is a simple ini-formatted file
; lines that start with semi-colons are comments.
; read `npm help config` for help on the various options
;;;;

//registry.npmjs.org/:_authToken=3b537d47-813f-42f8-9ee0-8f8d8c87d541

;;;;
; all options with default values
;;;;
; access=null
; allow-same-version=false
; always-auth=false
; also=null
; audit=true
; audit-level=low
; auth-type=legacy
; before=null
; bin-links=true
; browser=null
; ca=null
; cafile=undefined
; cache=/Users/arraybuffer/.npm
; cache-lock-stale=60000
; cache-lock-retries=10
; cache-lock-wait=10000
; cache-max=null
; cache-min=10
; cert=null
; cidr=null
; color=true
; depth=null
; description=true
; dev=false
; dry-run=false
; editor=vi
; engine-strict=false
; force=false
; fetch-retries=2
; fetch-retry-factor=10
; fetch-retry-mintimeout=10000
; fetch-retry-maxtimeout=60000
; git=git
; git-tag-version=true
; commit-hooks=true
; global=false
; globalconfig=/usr/local/etc/npmrc
; global-style=false
; group=20
; ham-it-up=false
; heading=npm
; if-present=false
; ignore-prepublish=false
; ignore-scripts=false
; init-module=/Users/arraybuffer/.npm-init.js
; init-author-name=
; init-author-email=
; init-author-url=
; init-version=1.0.0
; init-license=ISC
; json=false
; key=null
; legacy-bundling=false
; link=false
; local-address=undefined
; loglevel=notice
; logs-max=10
; long=false
; maxsockets=50
; message=%s
; metrics-registry=null
; node-options=null
; node-version=10.15.0
; offline=false
; onload-script=null
; only=null
; optional=true
; otp=null
; package-lock=true
; package-lock-only=false
; parseable=false
; prefer-offline=false
; prefer-online=false
; prefix=/usr/local
; preid=
; production=false
; progress=true
; proxy=null
; https-proxy=null
; noproxy=null
; user-agent=npm/{npm-version} node/{node-version} {platform} {arch}
; read-only=false
; rebuild-bundle=true
; registry=https://registry.npmjs.org/
; rollback=true
; save=true
; save-bundle=false
; save-dev=false
; save-exact=false
; save-optional=false
; save-prefix=^
; save-prod=false
; scope=
; script-shell=null
; scripts-prepend-node-path=warn-only
; searchopts=
; searchexclude=null
; searchlimit=20
; searchstaleness=900
; send-metrics=false
; shell=/bin/zsh
; shrinkwrap=true
; sign-git-commit=false
; sign-git-tag=false
; sso-poll-frequency=500
; sso-type=oauth
; strict-ssl=true
; tag=latest
; tag-version-prefix=v
; timing=false
; tmp=/var/folders/y6/wfrvn_7139xf0975fhllpw800000gn/T
; unicode=true
; unsafe-perm=true
; update-notifier=true
; usage=false
; user=0
; userconfig=/Users/arraybuffer/.npmrc
; umask=18
; version=false
; versions=false
; viewer=man
; _exit=true
; globalignorefile=/usr/local/etc/npmignore

关键信息:

cache=/Users/arraybuffer/.npm 				// npm 包缓存位置
prefix=/usr/local							  			// npm 全局安装路径的前缀
registry=https://registry.npmjs.org/  // npm 源配置地址 

修改 npm 全局安装和缓存文件的位置

默认情况下:

  • win系统下的路径基础部分是:%APPDATA%/npm/
  • mac系统下路径基础部分是:/usr/local/


默认的全局安装包位置:

  • win系统下路径是:%APPDATA%/npm/node_modules/
  • mac系统下路径是:/usr/local/lib/node_modules/


关于 npm 的全局文件位置及缓存文件位置,这两个路径实际上是可以修改的,并且需要注意的是:prefix 修改后,原来安装在默认目录下的npm包也还是生效的,并不需要再次安装:

npm config set prefix "<new_path>"
npm config set cache "<new_path>"

在我的Mac上,查看全局安装的包:

接着我修改全局安装路径(修改prefix):

npm config set prefix "/usr/local/src"
npm install -g less

可以查看上面的修改已经生效,另外当我们修改 prefix 后,安装全局的 node module,系统会自动生成目录:

所以全局 node_modules 的目录实际是:$prefix + 'lib/node_modules/'

posted on 2020-05-09 23:16  manbax  阅读(7802)  评论(0编辑  收藏  举报