NPM 私有倉庫的搭建

NPM 私有倉庫的搭建

爲何搭建私有倉庫

balabala,固然是有需求的時候嘛html

搭建流程

介紹和安裝verdaccionginx

備註:git

程序啓動後,配置文件爲/home/work/.config/verdaccio/config.yamlgithub

密碼文件所在位置:/home/work/.config/verdaccio/htpasswdweb

日誌文件所在文職:/home/work/verdaccio.lognpm

推薦博客1json

官方詳細文檔verdacciogitlab

  1. Nodejs環境全局安裝(root)網站

    npm install –g verdaccio
  2. pm2 運行程序,注意請切換成普通用戶work,不須要rootui

    網址訪問 http://localhost:4873

    pm2 start verdaccio
  3. 修改配置文件,配置文件以下,
    參照默認文件修改內容以下

    1. max_users: -1, 限制用戶自行註冊
    2. logs 日記存儲方式爲文件
    3. title 修改成公司名稱
    4. 修改packages的訪問,發佈,取消發佈的權限,都改成須要登陸的權限,初步限制僅公司內部認證過的帳戶可訪問(access: $authenticated,publish: $authenticated,unpublish: $authenticated,proxy: npmjs)
    5. 後期須要的話,可針對不一樣的倉庫配置不一樣的人員username權限,有開發能力的團隊,可自行定製插件中間件,好比限制IP和用戶名訪問

自定義UI界面

從官方ui主題倉庫fork到我的賬號,而後克隆到本地開始進行自定義修改

如今去 github fork?

自定義界面後須要發佈版本到npm中,根據配置文件的theme設定重啓後會自定應用

從官方倉庫中fock出來,修改搜索關鍵字favicon, title, logo等,footer和header也能夠修改一下

修改package.json中的name屬性,修改成 verdaccio-theme-****

安裝依賴,打包,等錄npm,發佈到npm倉庫(可發佈到共有npm,也能夠發佈到剛剛新起的npm私有倉庫服務)

npm install && npm run build && npm login && npm publish
npm i verdaccio-theme-**** -g
 
 

服務端安裝剛剛發佈的主題包(root, 全局安裝,由於verdaccio是動態加載插件)

最後在配置文件中修改主題配置

theme:
****:
a:b
su work
pm2 restart verdaccio 重啓服務生效


刷新頁面吧親。不知道爲何,僅寫theme: ****或者換行不生效,反正我也不懂,能用就行

添加用戶

點擊去建立htpasswd密碼文件網址

去網站生成密碼,而後將密碼添加到服務端密碼文件中,記得重啓服務

默認配置

 

#
# 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/verdaccio/verdaccio/tree/master/conf
#

# path to a directory with all packages
storage: ./storage
# path to a directory with plugins to include
plugins: ./plugins

web:
title: Qianjunet npm repo
# comment out to disable gravatar support
# gravatar: false
# by default packages are ordercer ascendant (asc|desc)
# sort_packages: asc

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: -1

# a list of other known repositories we can talk to
uplinks:
npmjs:
url: https://registry.npmjs.org/

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

'**':
# 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
access: $authenticated

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

# if package is not available locally, proxy requests to 'npmjs' registry
proxy: npmjs

# You can specify HTTP/1.1 server keep alive timeout in seconds for incoming connections.
# A value of 0 makes the http server behave similarly to Node.js versions prior to 8.0.0, which did not have a keep-alive timeout.
# WORKAROUND: Through given configuration you can workaround following issue https://github.com/verdaccio/verdaccio/issues/301. Set to 0 in case 60 is not enough.
server:
keepAliveTimeout: 60

middlewares:
audit:
enabled: true

# log settings
logs:
#- { type: stdout, format: pretty, level: http }
- {type: file, path: ./verdaccio.log, level: info}
#experiments:
# # support for npm token command
# token: false



配置nginx, 將請求轉發到4873服務

 

server {
listen 80;
server_name npm.**********.com;

location / {
proxy_http_version 1.1;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_pass http://127.0.0.1:4873$request_uri;
proxy_redirect off;
}
}

如何使用?nrm

  • npm i -g nrm

  • 查看當前全部npm的鏡像源

    nrm ls
  • 使用淘寶源

    1
    nrm use taobao
  • 添加咱們自定義的私有倉庫的源

    1
    nrm add ******** http://npm.*****.com
  • 使用自定義的源

    1
    nrm use ********
  • 添加錯誤後,可刪除源

    1
    nrm del *****
  • 切換到自定義npm源後,使用npm登陸

    1
    2
    3
    4
    5
    6
    nrm use ******
    npm login
    input username:
    input password:
    input public email:
    輸入相關信息後登陸成功,登陸成功便可使用npm install some-package-name

發佈package

1
2
3
4
5
npm init
code .......
build .....
npm login
npm publish

最終詳細配置,仍是得看官網,雖然官網的描述很很差懂,並且不全,可是比本篇記錄要全,畢竟這個只是小哥哥看完文檔,實際操做後的隨筆而已

TODO: 待對接npm的用戶信息到https://gitlab.*****.com

相關文章
相關標籤/搜索