指定本地私有倉(cāng)庫(kù)

以下是配置 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文件

  1. 打開全局配置文件(路徑根據(jù)系統(tǒng)不同):

    # Windows
    notepad %USERPROFILE%\.npmrc
    
    # macOS/Linux
    nano ~/.npmrc
    
  2. 添加以下內(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)

  1. 密碼保護(hù)

    • 避免將 .npmrc提交到 Git(可添加到 .gitignore
    • Nexus 建議使用 Token 認(rèn)證替代明文密碼
  2. 企業(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 installnpm 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)。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容