系統(tǒng):Ubuntu
該私有倉(cāng)庫(kù)可以放私有包
,也可以自動(dòng)緩存Npm官方庫(kù)上的包
一、安裝
$ npm install sinopia -g --no-optional --no-shrinkwrap
$ npm i sinopia -g ## 這種方式易報(bào)錯(cuò)
二、報(bào)錯(cuò)處理
- 錯(cuò)誤一:python:請(qǐng)安裝
$ apt-get install python
- 錯(cuò)誤二:
make g++ command not found
: 請(qǐng)安裝:sudo apt-get install g++
- 錯(cuò)誤三:
make: *** [Release/obj.target/fs-ext/fs-ext.o] Error 1
: 請(qǐng)如此安裝: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 這個(gè)包 Sinopia并不需要,所以為了避免它導(dǎo)致該錯(cuò)誤,用npm install sinopia -g --no-optional --no-shrinkwrap
這種方式安裝。
錯(cuò)誤三詳見:github-上的解釋
三、運(yùn)行
wxq@ubuntu:~$ sinopia
### 會(huì)輸出:
warn --- config file - /home/wxq/.config/sinopia/config.yaml ## sinopia的配置文件所在位置
warn --- http address - http://localhost:4873/ ## 你的npm私有庫(kù)的訪問地址
...
- 本地訪問私有庫(kù)地址
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:
## 在私有倉(cāng)庫(kù)添加賬戶時(shí),賬戶信息的存放位置。
file: ./htpasswd
# Maximum amount of users allowed to register, defaults to "+inf".
# You can set this to -1 to disable registration.
## npm私庫(kù)允許注冊(cè)的最大用戶數(shù),如果設(shè)置為-1則表示禁止注冊(cè)。
#max_users: 1000
# a list of other known repositories we can talk to
uplinks:
## npm私庫(kù) 依賴的第三方npm庫(kù)的地址,可以配置其他的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
# 如果本地倉(cāng)庫(kù)的包不可用,則從上面配置的第三方npmjs源中進(jìn)行獲取。
proxy: npmjs
# log settings
logs:
- {type: stdout, format: pretty, level: http}
#- {type: file, path: sinopia.log, level: info}
## 這里配置sinopia的監(jiān)聽I(yíng)P,默認(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的鏡像倉(cāng)庫(kù)地址
$ npm i nrm -g
$ nrm add sinopia http://localhost:4873 ## 添加新搭建的npm私有倉(cāng)庫(kù)地址
### 其他相關(guān)命令:
$ nrm --help 查看
$ nrm ls ## 列出所有的可用鏡像倉(cāng)庫(kù)地址
五、sinopia使用
1、npm 下載包
切換為新搭建的私有倉(cāng)庫(kù)后,npm的使用方式?jīng)]有任何改變,仍是使用npm install xxx
安裝我們所需要的包。
如果私有倉(cāng)庫(kù)中沒有所需要的包,會(huì)從備用的鏡像中下載并緩存到本地,下一次在進(jìn)行安裝時(shí)會(huì)直接從私有倉(cāng)庫(kù)中獲取。
2、npm 發(fā)布包
要在私有npm倉(cāng)庫(kù)中發(fā)布包首先需要注冊(cè)或登陸賬號(hào)。
- 1、需要先登錄
1)、如果還沒有賬號(hào),通過輸入命令
npm adduser --registry <指定你的賬號(hào)要添加到的NPM源:如:http://localhost:4873/>
然后依次輸入用戶名,密碼,郵箱用戶即可創(chuàng)建完畢。
2)、如果已有賬號(hào),通過輸入命令
npm login --registry http://localhost:4873/
然后依次輸入用戶名,密碼,郵箱用戶即可登陸。
3)、退出登錄
npm logout --registry http://localhost:4873/
- 2、執(zhí)行初始化。
$ npm init
這個(gè)過程中要輸入項(xiàng)目名,版本號(hào),作者,開源協(xié)議等信息,自動(dòng)生成package.json文件。
這里可以在這里填寫相關(guān)信息或者直接回車跳過,因?yàn)楹罄m(xù)可以直接修改package.json文件。
此外,通過在目錄內(nèi)新建README文件,可添加包的使用說明和用例代碼。README文件支持markdown,書寫十分方便。
- 3、發(fā)布包到私有npm倉(cāng)庫(kù)了。
npm publish --registry <你的私有倉(cāng)庫(kù)地址>
3、發(fā)布私有包
所有的私有模塊都是 scoped package 的.
scope 是 npm 的新特性。如果一個(gè)模塊的名字以 "@" 開始,那么他就是一個(gè)scoped package。scope 就是"@"與"/"之間的部分。
@scope/project-name
當(dāng)你注冊(cè)私有模塊到一個(gè)用戶下時(shí),你的 scope 就是當(dāng)前用戶的用戶名。
@username/project-name
如果要使用 npm init 初始化一個(gè)軟件包,你可以通過自定義 --scope 選項(xiàng)設(shè)置你的 scope
npm init --scope=<your_scope>
如果你在大多數(shù)時(shí)候使用的 scope 都是相同的,可以設(shè)置一個(gè)默認(rèn)的 scope ,這樣在我們初始化的時(shí)候會(huì)自動(dòng)使用該 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.lxweimin.com/p/e4db4a0af96a
六、Sinopia的config.yaml文件詳解:
config.yaml是sinopia的配置文件
1>其常用的配置
storage: 倉(cāng)庫(kù)保存的地址,publish時(shí)倉(cāng)庫(kù)保存的地址。
auth: htpasswd file:賬號(hào)密碼的文件地址,初始化時(shí)不存在,可指定需要手工創(chuàng)建。
max_users:默認(rèn)1000,為允許用戶注冊(cè)的數(shù)量。
為-1時(shí),不允許用戶通過npm adduser注冊(cè)。
但是,當(dāng)為-1時(shí),可以通過直接編寫htpasswd file內(nèi)容的方式添加用戶。
語(yǔ)法:用戶名:{SHA}哈希加密的字符=:autocreated 時(shí)間
加密算法:SHA1哈稀之后再轉(zhuǎn)換成 Base64 輸出就好
uplinks: 配置上游的npm服務(wù)器,主要用于請(qǐng)求的倉(cāng)庫(kù)不存在時(shí)到上游服務(wù)器去拉取。
packages: 配置模塊。access訪問下載權(quán)限,publish包的發(fā)布權(quán)限。
格式如下:
scope:
**權(quán)限**:**操作**
scope:兩種模式
一種是 @/ 表示某下屬的某項(xiàng)目
另一種是 * 匹配項(xiàng)目名稱(名稱在package.json中有定義)
權(quán)限:
l access: 表示哪一類用戶可以對(duì)匹配的項(xiàng)目進(jìn)行安裝(install)
l publish: 表示哪一類用戶可以對(duì)匹配的項(xiàng)目進(jìn)行發(fā)布(publish)
l proxy: 如其名,這里的值是對(duì)應(yīng)于 uplinks 的名稱,如果本地不存在,允許去對(duì)應(yīng)的uplinks去取。
操作:
l $all 表示所有人(已注冊(cè)、未注冊(cè))都可以執(zhí)行對(duì)應(yīng)的操作
l $authenticated 表示只有通過驗(yàn)證的人(已注冊(cè))可以執(zhí)行對(duì)應(yīng)操作,注意,任何人都可以去注冊(cè)賬戶。
l $anonymous 表示只有匿名者可以進(jìn)行對(duì)應(yīng)操作(通常無用)
l 或者也可以指定對(duì)應(yīng)于之前我們配置的用戶表 htpasswd 中的一個(gè)或多個(gè)用戶,這樣就明確地指定哪些用戶可以執(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
#倉(cāng)庫(kù)
# 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è)置請(qǐng)求無應(yīng)答超時(shí)時(shí)間
# 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)為最新的時(shí)間為2分鐘,2分鐘同一個(gè)數(shù)據(jù)的請(qǐng)求不會(huì)向上游服務(wù)器請(qǐng)求
# 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ù),就停止一段時(shí)間不訪問上游服務(wù)器,默認(rèn)是2次不應(yīng)答,5分鐘不去請(qǐng)求
# 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去請(qǐng)求
# 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