搭建npm私服

1.安裝npm私服服務(wù)github地址

npm install -g sinopia  

2.啟動(dòng)sinopia

sinopia
  • 你會(huì)看到下面兩行提示:
// sinopia 的配置文件所在路徑
warn  --- config file - /root/.config/sinopia/htpassw

//sinopia 服務(wù)的域名和端口號(hào)
warn  --- http address - http://localhost:4873/

3.守護(hù)進(jìn)程

  • 因?yàn)閚ode服務(wù)非常脆弱,一般在實(shí)際中使用都會(huì)配合守護(hù)進(jìn)程。這里我用的是 pm2 做守護(hù)進(jìn)程

1.安裝PM2

npm install -g pm2

2.通過(guò) PM2 啟動(dòng) sinopia

pm2 start `which sinopia`

4.修改sinopia配置文件

#
# 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   #保存用戶的賬號(hào)密碼等信息
    # Maximum amount of users allowed to register, defaults to "+inf".
    # You can set this to -1 to disable registration.
    max_users: -1  #默認(rèn)為1000,改為-1,禁止注冊(cè)

# a list of other known repositories we can talk to
uplinks:
  npmjs:
    url: http://registry.npm.taobao.org/  #默認(rèn)為npm的官網(wǎng),由于國(guó)情,修改 url 讓sinopia使用 淘寶的npm鏡像地址
    
packages:  #配置權(quán)限管理
  '@*/*':
    # scoped packages
    access: $all  #表示哪一類(lèi)用戶可以對(duì)匹配的項(xiàng)目進(jìn)行安裝 【$all 表示所有人都可以執(zhí)行對(duì)應(yīng)的操作,$authenticated 表示只有通過(guò)驗(yàn)證的人可以執(zhí)行對(duì)應(yīng)操作,$anonymous 表示只有匿名者可以進(jìn)行對(duì)應(yīng)操作(通常無(wú)用)】
    publish: $authenticated  #表示哪一類(lèi)用戶可以對(duì)匹配的項(xiàng)目進(jìn)行發(fā)布

  '*':
    # 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  #表示哪一類(lèi)用戶可以對(duì)匹配的項(xiàng)目進(jìn)行安裝

    # allow all known users to publish packages
    # (anyone can register by default, remember?)
    publish: $authenticated  #表示哪一類(lèi)用戶可以對(duì)匹配的項(xiàng)目進(jìn)行發(fā)布

    # if package is not available locally, proxy requests to 'npmjs' registry
    proxy: npmjs  #如其名,這里的值是對(duì)應(yīng)于 uplinks

# 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  #默認(rèn)沒(méi)有,只能在本機(jī)訪問(wèn),添加后可以通過(guò)外網(wǎng)訪問(wèn)

(原文參考:https://segmentfault.com/a/1190000005790827

5.添加用戶

npm adduser

6.發(fā)布包

// 每次發(fā)布之前要用命令登錄
npm login
//初始化包
npm init
//發(fā)布包
npm publish

可以在http://localhost:4873/ 查看包的詳情

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容