npm私有倉庫搭建


背景

  1. Node.js開發本地項目,有時不同項目之間存在依賴,如果不想把項目發布到npm社區的倉庫,則需要有自己本地的倉庫。
  2. 有些公司采用的是內網開發,很多npm資源無法從內網去下載。

sinopia(主流)

使用文件系統作為存儲,僅保存用戶需要的包,如果本地倉庫沒有對應的包,則從指定的registry下載,默認為npmjs.org,可以改成淘寶的鏡像。

安裝

Sinopia的安裝比較簡單,只需使用npm一條安裝命令即可:

npm install -g sinopia 或者 yarn global add sinopia 

安裝遇到問題

1.python

gyp ERR! stack Error: Can't find Python executable "python", you can set the PYTHON env variable.

node-gyp依賴python 2.7。安裝python2.7,並把它添加到環境變量PATH,npm config set python python2.7
2.不支持fs-ext和crypt3

只要有出現以上圖的或者 其他關於crypt3的問題的話, 看下面解決方法

查看sinopia源碼(看你安裝在哪)里的package.yaml

optionalDependencies:
  # those are native modules that could fail to compile
  # and unavailable on windows
  fs-ext: '>=0.4.1 <1.0.0-0'
  crypt3: '>=0.1.6 <1.0.0-0' # for sinopia-htpasswd

刪除sinopia安裝目錄node_modules里的fs-ext和crypt3相關的包,否則執行npm添加用戶和登陸驗證時會報錯。包括:.0.2.0@crypt3,.0.6.0@fs-ext,crypt3和fs-ext以及sinopia-htpasswd\node_modules下的crypt3和fs-ext

使用

啟動: sinopia

你也可以打開 地址看一下http://localhost:4873/

就說明成功了

換npm 源

npm config set registry http://xxx.xx.xx.xx:4873/       
//xxx.xx.xx.xx 為自己的ip 

建議使用用 nrm

npm install -g nrm # 安裝nrm
nrm add name http://XXXXXX:4873 # 添加本地的npm鏡像地址
nrm use name # 使用本址的鏡像地址     name為你要增加的地址

新建用戶

npm adduser
Username: test
Password: test
Email: (this IS public) xxx@xxxx

然后就可以發布包了

npm publish     // 在自己要發布的包 路徑打這個命令

這邊發布的包可以 是自己寫的一些資源 或者是其他的 但是要根據npm 包的規范

本人配置文件

#
# This is the default config file. It allows all users to do anything,
# so don't use it on production systems.
#
# Look here for more config file examples:
# https://github.com/rlidwka/sinopia/tree/master/conf
#

# path to a directory with all packages
storage: ./storage    # npm包存放的路徑

auth:
  htpasswd:
    file: ./htpasswd
    # Maximum amount of users allowed to register, defaults to "+inf".
    # You can set this to -1 to disable registration.
    # max_users: 1000
    max_users: 1000     # 默認為1000,改為-1,禁止注冊

# a list of other known repositories we can talk to
uplinks:
  npmjs:
    url: http://registry.npm.taobao.org/  # 默認為npm的官網,由於國情,修改 url 讓sinopia使用 淘寶的npm鏡像地址

packages:
  '@*/*':
    # scoped packages
    access: $all
    publish: $authenticated

  '*':
    # allow all users (including non-authenticated users) to read and
    # publish all packages
    #
    # you can specify usernames/groupnames (depending on your auth plugin)
    # and three keywords: "$all", "$anonymous", "$authenticated"
    access: $all

    # allow all known users to publish packages
    # (anyone can register by default, remember?)
    publish: $authenticated

    # if package is not available locally, proxy requests to 'npmjs' registry
    # proxy: npmjs   #這個去掉的話,sinopia 將不去下載依賴包   如果只是要放自己資源倉庫的話就去掉      
    # 

# log settings
logs:
  - {type: stdout, format: pretty, level: http}
  #- {type: file, path: sinopia.log, level: info}

# you can specify listen address (or simply a port) 
listen: 0.0.0.0:4873  # 默認沒有,只能在本機訪問,添加后可以通過外網訪問。

注意:配置文件 是在你的用戶名 如:本人就admin 找到目錄就可以了 (win)

storage: 倉庫保存的路徑
htpasswd: 保存密碼信息 只有新建用戶后才 有這個文件
config.yaml: 這個是本地的 配置文件 信息(改這個)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM