以下是配置 npm 全局使用本地 Nexus 私有倉(cāng)庫(kù)(含認(rèn)證信息)的完整步驟:
?? 全局配置本地倉(cāng)庫(kù)地址
npm config set registry http://192.168.0.12/nexus/repository/npm-public/
配置認(rèn)證信息(兩種方式任選)
方式1:通過(guò)命令行直接配置(推薦)
npm config set //192.168.0.12/nexus/repository/npm-public/:_password="dev@123"
npm config set //192.168.0.12/nexus/repository/npm-public/:_username="dev"
npm config set //192.168.0.12/nexus/repository/npm-public/:always-auth=true
方式2:手動(dòng)編輯全局 .npmrc文件
-
打開全局配置文件(路徑根據(jù)系統(tǒng)不同):
# Windows notepad %USERPROFILE%\.npmrc # macOS/Linux nano ~/.npmrc -
添加以下內(nèi)容(注意地址格式):
registry=http://192.168.0.12/nexus/repository/npm-public/ //192.168.0.12/nexus/repository/npm-public/:_auth=ZGV2OmRldkAxMjM= always-auth=true-
_auth值是dev:dev@123的 Base64 編碼,可通過(guò)以下命令生成:echo -n "dev:dev@123" | base64 # 輸出:ZGV2OmRldkAxMjM=
-
驗(yàn)證配置是否生效
npm config list | grep -E 'registry|auth'
應(yīng)輸出類似:
registry = "http://192.168.0.12/nexus/repository/npm-public/"
//192.168.0.12/nexus/repository/npm-public/:always-auth = true
//192.168.0.12/nexus/repository/npm-public/:_auth = ZGV2OmRldkAxMjM=
測(cè)試私有倉(cāng)庫(kù)連接
npm ping --registry http://192.168.0.12/nexus/repository/npm-public/
成功會(huì)返回:
npm notice PING http://192.168.0.12/nexus/repository/npm-public/
npm notice 200 OK
安全注意事項(xiàng)
-
密碼保護(hù):
- 避免將
.npmrc提交到 Git(可添加到.gitignore) - Nexus 建議使用 Token 認(rèn)證替代明文密碼
- 避免將
-
企業(yè)級(jí)安全:
//192.168.0.12/nexus/repository/npm-public/:_authToken=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
恢復(fù)默認(rèn)倉(cāng)庫(kù)(如需撤銷)
npm config delete registry
npm config delete //192.168.0.12/nexus/repository/npm-public/:_auth
npm config delete always-auth
通過(guò)以上配置,所有 npm 命令(如 npm install、npm publish)都會(huì)自動(dòng)使用你的本地 Nexus 倉(cāng)庫(kù)。如果遇到 SSL 證書問(wèn)題,可臨時(shí)添加 npm config set strict-ssl false,但生產(chǎn)環(huán)境不建議禁用 SSL 校驗(yàn)。