Sinopia---構(gòu)建私有NPM倉庫

系統(tǒng):Ubuntu
該私有倉庫可以放私有包昔搂,也可以自動緩存Npm官方庫上的包

一赎败、安裝

$ npm install sinopia -g --no-optional --no-shrinkwrap
$ npm i sinopia -g  ## 這種方式易報錯

二函筋、報錯處理

  • 錯誤一:python:請安裝$ apt-get install python
  • 錯誤二:make g++ command not found : 請安裝:sudo apt-get install g++
  • 錯誤三:make: *** [Release/obj.target/fs-ext/fs-ext.o] Error 1: 請如此安裝:npm install sinopia -g --no-optional --no-shrinkwrap

Dependencies crypt3 and fs-ext are native modules that could fail to compile and unavailable on Windows. They are optional and does not affect usage of Sinopia. To avoid errors, you can not install this dependencies with no-optional and no-shrinkwrap options.
貌似是說:crypt3 這個包 Sinopia并不需要盟步,所以為了避免它導(dǎo)致該錯誤,用npm install sinopia -g --no-optional --no-shrinkwrap 這種方式安裝。
錯誤三詳見:github-上的解釋

三馋艺、運(yùn)行

wxq@ubuntu:~$ sinopia
### 會輸出:
 warn  --- config file  - /home/wxq/.config/sinopia/config.yaml  ## sinopia的配置文件所在位置
 warn  --- http address - http://localhost:4873/  ## 你的npm私有庫的訪問地址
...
  • 本地訪問私有庫地址

1、配置文件:

默認(rèn)路徑:~/.config/sinopia/config.yaml迈套。

#
# 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
#

# 依賴包的存放位置
storage: /home/wxq/sinopia/storage

auth:
  htpasswd:
    ## 在私有倉庫添加賬戶時捐祠,賬戶信息的存放位置。
    file: ./htpasswd    
    # Maximum amount of users allowed to register, defaults to "+inf".
    # You can set this to -1 to disable registration.
    ## npm私庫允許注冊的最大用戶數(shù)桑李,如果設(shè)置為-1則表示禁止注冊踱蛀。
    #max_users: 1000

# a list of other known repositories we can talk to
uplinks:
  ## npm私庫 依賴的第三方npm庫的地址,可以配置其他的npm源(如:cnpm)贵白。
  npmjs:
    url: https://registry.npmjs.org/

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
    # 如果本地倉庫的包不可用率拒,則從上面配置的第三方npmjs源中進(jìn)行獲取。
    proxy: npmjs

# log settings
logs:
  - {type: stdout, format: pretty, level: http}
  #- {type: file, path: sinopia.log, level: info}
## 這里配置sinopia的監(jiān)聽IP禁荒,默認(rèn)是監(jiān)聽在 127.0.0.1:4873的猬膨,改成0.0.0.0:4873的話,就可以被外網(wǎng)訪問了(當(dāng)然也可以借助Nginx來處理)呛伴。
listen: 0.0.0.0:4873

四勃痴、Npm鏡像地址管理:Nrm

用于管理npm的鏡像倉庫地址

$ npm i nrm -g
$ nrm add sinopia http://localhost:4873   ## 添加新搭建的npm私有倉庫地址

### 其他相關(guān)命令:
$ nrm --help 查看
$ nrm ls  ## 列出所有的可用鏡像倉庫地址

五谒所、sinopia使用

1、npm 下載包

切換為新搭建的私有倉庫后召耘,npm的使用方式?jīng)]有任何改變,仍是使用npm install xxx安裝我們所需要的包褐隆。
如果私有倉庫中沒有所需要的包污它,會從備用的鏡像中下載并緩存到本地,下一次在進(jìn)行安裝時會直接從私有倉庫中獲取庶弃。

2衫贬、npm 發(fā)布包

要在私有npm倉庫中發(fā)布包首先需要注冊或登陸賬號。

  • 1歇攻、需要先登錄

1)固惯、如果還沒有賬號,通過輸入命令

npm adduser --registry <指定你的賬號要添加到的NPM源:如:http://localhost:4873/>

然后依次輸入用戶名缴守,密碼葬毫,郵箱用戶即可創(chuàng)建完畢。
2)屡穗、如果已有賬號贴捡,通過輸入命令

npm login --registry http://localhost:4873/

然后依次輸入用戶名,密碼村砂,郵箱用戶即可登陸烂斋。
3)、退出登錄

npm logout --registry http://localhost:4873/
  • 2础废、執(zhí)行初始化汛骂。
$ npm init

這個過程中要輸入項(xiàng)目名,版本號评腺,作者帘瞭,開源協(xié)議等信息,自動生成package.json文件蒿讥。
這里可以在這里填寫相關(guān)信息或者直接回車跳過图张,因?yàn)楹罄m(xù)可以直接修改package.json文件。
此外诈悍,通過在目錄內(nèi)新建README文件祸轮,可添加包的使用說明和用例代碼。README文件支持markdown侥钳,書寫十分方便适袜。

  • 3、發(fā)布包到私有npm倉庫了舷夺。
npm publish --registry <你的私有倉庫地址>

3苦酱、發(fā)布私有包

所有的私有模塊都是 scoped package 的.

scope 是 npm 的新特性售貌。如果一個模塊的名字以 "@" 開始,那么他就是一個scoped package疫萤。scope 就是"@"與"/"之間的部分颂跨。

@scope/project-name

當(dāng)你注冊私有模塊到一個用戶下時,你的 scope 就是當(dāng)前用戶的用戶名扯饶。

@username/project-name

如果要使用 npm init 初始化一個軟件包恒削,你可以通過自定義 --scope 選項(xiàng)設(shè)置你的 scope

npm init --scope=<your_scope>

如果你在大多數(shù)時候使用的 scope 都是相同的,可以設(shè)置一個默認(rèn)的 scope 尾序,這樣在我們初始化的時候會自動使用該 scope 钓丰。

npm config set scope <your_scope>

發(fā)布:

npm publish
  • image.png

4、更新發(fā)布

npm更新包和發(fā)布包的命令是一樣的每币,都是npm publish,不同之處在于携丁,你需要修改包的版本。

所以步驟是:

  • 1.修改包的版本(package.json里的version字段)
  • 2.npm publish

參考:
http://www.cnblogs.com/kelsen/p/4964574.html
http://www.reibang.com/p/e4db4a0af96a

六兰怠、Sinopia的config.yaml文件詳解:

config.yaml是sinopia的配置文件

1>其常用的配置

storage: 倉庫保存的地址梦鉴,publish時倉庫保存的地址。

auth: htpasswd file:賬號密碼的文件地址揭保,初始化時不存在尚揣,可指定需要手工創(chuàng)建。

           max_users:默認(rèn)1000掖举,為允許用戶注冊的數(shù)量快骗。

                為-1時,不允許用戶通過npm adduser注冊塔次。

              但是方篮,當(dāng)為-1時,可以通過直接編寫htpasswd file內(nèi)容的方式添加用戶励负。

語法:用戶名:{SHA}哈希加密的字符=:autocreated 時間

加密算法:SHA1哈稀之后再轉(zhuǎn)換成 Base64 輸出就好

uplinks: 配置上游的npm服務(wù)器藕溅,主要用于請求的倉庫不存在時到上游服務(wù)器去拉取。

packages: 配置模塊继榆。access訪問下載權(quán)限,publish包的發(fā)布權(quán)限巾表。

  格式如下:

scope:

      **權(quán)限**:**操作**

scope:兩種模式

一種是 @/ 表示某下屬的某項(xiàng)目

另一種是 * 匹配項(xiàng)目名稱(名稱在package.json中有定義)

權(quán)限:

l access: 表示哪一類用戶可以對匹配的項(xiàng)目進(jìn)行安裝(install)

l publish: 表示哪一類用戶可以對匹配的項(xiàng)目進(jìn)行發(fā)布(publish)

l proxy: 如其名,這里的值是對應(yīng)于 uplinks 的名稱略吨,如果本地不存在集币,允許去對應(yīng)的uplinks去取。

操作:

l $all 表示所有人(已注冊翠忠、未注冊)都可以執(zhí)行對應(yīng)的操作

l $authenticated 表示只有通過驗(yàn)證的人(已注冊)可以執(zhí)行對應(yīng)操作鞠苟,注意,任何人都可以去注冊賬戶。

l $anonymous 表示只有匿名者可以進(jìn)行對應(yīng)操作(通常無用)

l 或者也可以指定對應(yīng)于之前我們配置的用戶表 htpasswd 中的一個或多個用戶当娱,這樣就明確地指定哪些用戶可以執(zhí)行匹配的操作

listen:配置監(jiān)聽端口和主機(jī)名吃既。

   localhost:4873 #默認(rèn)

   0.0.0.0:4873 #在所有網(wǎng)卡監(jiān)聽

代理:

#http_proxy: [http://something.local/](http://something.local/)  #http代理

#https_proxy: [https://something.local/](https://something.local/)  #https代理

#no_proxy: localhost,127.0.0.1  #不適用代理的iP

修改了配置文件后,運(yùn)行命令

$ sinopia -c config.yml

2>附錄github中比較全的配置和說明

https://raw.githubusercontent.com/rlidwka/sinopia/master/conf/full.yaml

#倉庫
# path to a directory with all packages
storage: ./storage

# a list of users
#
# This could be deprecated soon, use auth plugins instead (see htpasswd below).
users:
  admin:
    # crypto.createHash('sha1').update(pass).digest('hex')
    password: a94a8fe5ccb19ba61c4c0873d391e987982fbbd3

#是否支持web接口
web:
  # web interface is disabled by default in 0.x, will be enabled soon in 1.x
  # when all its issues will be fixed
  #
  # set this to `true` if you want to experiment with web ui now;
  # this has a lot of issues, e.g. no auth yet, so use at your own risk
  #enable: true title: Sinopia
  # logo: logo.png
  # template: custom.hbs

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 # a list of other known repositories we can talk to
#上游npm服務(wù)器配置
uplinks:
  npmjs:
    url: https://registry.npmjs.org/
 #設(shè)置請求無應(yīng)答超時時間
    # amount of time to wait for repository to respond
    # before giving up and use the local cached copy
    #timeout: 30s

#設(shè)置數(shù)據(jù)認(rèn)為最新的時間為2分鐘跨细,2分鐘同一個數(shù)據(jù)的請求不會向上游服務(wù)器請求
    # maximum time in which data is considered up to date #
    # default is 2 minutes, so server won‘t request the same data from
    # uplink if a similar request was made less than 2 minutes ago
    #maxage: 2m

#設(shè)置訪問失敗達(dá)到某次數(shù)鹦倚,就停止一段時間不訪問上游服務(wù)器,默認(rèn)是2次不應(yīng)答冀惭,5分鐘不去請求
    # if two subsequent requests fail, no further requests will be sent to
    # this uplink for five minutes
    #max_fails: 2 #fail_timeout: 5m

    # timeouts are defined in the same way as nginx, see:
    # http://wiki.nginx.org/ConfigNotation
 #包權(quán)限配置
packages:
  # uncomment this for packages with "local-" prefix to be available
  # for admin only, it‘s a recommended way of handling private packages
  #'local-*':
  #  access: admin
  #  publish: admin
  #  # you can override storage directory for a group of packages this way:
  #  storage: 'local_storage'

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

    # allow ‘a(chǎn)dmin‘ to publish packages
    publish: admin

#如果包本地不存在震叙,則去npmjs去請求
    # if package is not available locally, proxy requests to ‘npmjs‘ registry
    proxy: npmjs

#####################################################################
# Advanced settings
#####################################################################

# if you use nginx with custom path, use this to override links
#url_prefix: https://dev.company.local/sinopia/
 # You can specify listen address (or simply a port).
# If you add multiple values, sinopia will listen on all of them.
#
# Examples:
#
#listen:
# - localhost:4873 # default value
# - http://localhost:4873     # same thing
# - 0.0.0.0:4873 # listen on all addresses (INADDR_ANY)
# - https://example.org:4873  # if you want to use https
# - [::1]:4873 # ipv6
# - unix:/tmp/sinopia.sock    # unix socket

#https證書配置(listen需要設(shè)置成https)
# Configure HTTPS, it is required if you use "https" protocol above.
#https:
#  key: path/to/server.key
#  cert: path/to/server.crt

# type: file | stdout | stderr
# level: trace | debug | info | http (default) | warn | error | fatal
#
# parameters for file: name is filename
#  {type: ‘file‘, path: ‘sinopia.log‘, level: ‘debug‘},
#
# parameters for stdout and stderr: format: json | pretty
#  {type: ‘stdout‘, format: ‘pretty‘, level: ‘debug‘},
logs: - {type: stdout, format: pretty, level: http}
  #- {type: file, path: sinopia.log, level: info}

#代理設(shè)置
# you can specify proxy used with all requests in wget-like manner here
# (or set up ENV variables with the same name)
#http_proxy: http://something.local/
#https_proxy: https://something.local/
#no_proxy: localhost,127.0.0.1 #設(shè)置json文檔大小上限
# maximum size of uploaded json document
# increase it if you have "request entity too large" errors
#max_body_size: 1mb

# Workaround for countless npm bugs. Must have for npm <1.14.x, but expect
# it to be turned off in future versions. If `true`, latest tag is ignored,
# and the highest semver is placed instead.
#ignore_latest_tag: false
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市云头,隨后出現(xiàn)的幾起案子捐友,更是在濱河造成了極大的恐慌淫半,老刑警劉巖溃槐,帶你破解...
    沈念sama閱讀 211,265評論 6 490
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異科吭,居然都是意外死亡昏滴,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,078評論 2 385
  • 文/潘曉璐 我一進(jìn)店門对人,熙熙樓的掌柜王于貴愁眉苦臉地迎上來谣殊,“玉大人,你說我怎么就攤上這事牺弄∫黾福” “怎么了?”我有些...
    開封第一講書人閱讀 156,852評論 0 347
  • 文/不壞的土叔 我叫張陵势告,是天一觀的道長蛇捌。 經(jīng)常有香客問我,道長咱台,這世上最難降的妖魔是什么络拌? 我笑而不...
    開封第一講書人閱讀 56,408評論 1 283
  • 正文 為了忘掉前任,我火速辦了婚禮回溺,結(jié)果婚禮上春贸,老公的妹妹穿的比我還像新娘。我一直安慰自己遗遵,他們只是感情好萍恕,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,445評論 5 384
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著车要,像睡著了一般雄坪。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 49,772評論 1 290
  • 那天维哈,我揣著相機(jī)與錄音绳姨,去河邊找鬼。 笑死阔挠,一個胖子當(dāng)著我的面吹牛飘庄,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播购撼,決...
    沈念sama閱讀 38,921評論 3 406
  • 文/蒼蘭香墨 我猛地睜開眼跪削,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了迂求?” 一聲冷哼從身側(cè)響起碾盐,我...
    開封第一講書人閱讀 37,688評論 0 266
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎揩局,沒想到半個月后毫玖,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 44,130評論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡凌盯,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,467評論 2 325
  • 正文 我和宋清朗相戀三年付枫,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片驰怎。...
    茶點(diǎn)故事閱讀 38,617評論 1 340
  • 序言:一個原本活蹦亂跳的男人離奇死亡阐滩,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出县忌,到底是詐尸還是另有隱情掂榔,我是刑警寧澤,帶...
    沈念sama閱讀 34,276評論 4 329
  • 正文 年R本政府宣布症杏,位于F島的核電站装获,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏鸳慈。R本人自食惡果不足惜饱溢,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,882評論 3 312
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望走芋。 院中可真熱鬧绩郎,春花似錦、人聲如沸翁逞。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,740評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽挖函。三九已至状植,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背津畸。 一陣腳步聲響...
    開封第一講書人閱讀 31,967評論 1 265
  • 我被黑心中介騙來泰國打工振定, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人肉拓。 一個月前我還...
    沈念sama閱讀 46,315評論 2 360
  • 正文 我出身青樓后频,卻偏偏與公主長得像,于是被迫代替她去往敵國和親暖途。 傳聞我的和親對象是個殘疾皇子卑惜,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,486評論 2 348