sinopia 是一個零配置帶緩存功能的 npm 包管理工具。
sinopia 有以下幾個優勢值得關注:
- 不同步拉取 npm 庫,占據大量硬盤,沒有硬盤被撐爆的問題;
- 安裝配置極其簡單,不需要數據庫;
- 支持配置上游 registry 配置,一次拉取即緩存;
- 支持 forever 及 pm2 守護進程管理;
服務器部署
安裝
> npm install -g sinopia
啟動
> sinopia
warn --- config file - C:\Users\jason\AppData\Roaming\sinopia\config.yaml
warn --- http address - http://localhost:4873/
打開 http://localhost:4873/ 如果能正常顯示,說明安裝成功。

sinopia 啟動時默認會創建 config.yaml
文件,文件路徑可以看輸出的提示。我們將上面路徑的 config.yaml 拷貝到指定盤符的文件夾下,使用 -c
選項指定配置文件,並執行命令:
> sinopia -c D:\sinopia\config.yaml
現在我們就可以在指定目錄下運行了,以后上傳的 npm 包也會放在這個目錄中。
配置
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 # # 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: -1 //默認為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 # 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 ////默認沒有,只能在本機訪問,添加后可以通過外網訪問。
客戶端使用
全局安裝 nrm
可以快速修改/切換/增加 npm 鏡像地址:
> npm install -g nrm # 安裝nrm > nrm add XXXX http://XXXXXX:4873 # 添加私有 npm 鏡像地址 > nrm use XXXX # 使用私有鏡像地址
注意: 不建議使用私有源安裝公網 npm 包,安裝公網 npm 包請使用 taobao 源
- 需要安裝私有 npm 包時,請執行
nrm use sinopia
切換到私有 npm 源- 安裝公有 npm 包時,請執行
nrm use taobao
切換到淘寶源安裝
nrm 其他命令:
> nrm --help # 查看nrm命令幫助 > nrm list # 列出可用的 npm 鏡像地址 > nrm use taobao # 使用`淘寶npm`鏡像地址
常用 npm 命令
注冊發布者
> npm adduser # 按提示輸入用戶名,密碼,郵箱即可
登陸 npm 源
> npm login # 按提示輸入用戶名,密碼,郵箱即可
發布 npm 包
> cd D:\projects\yourproject # 進入項目目錄 > npm publish # 執行發布命令
刪除 npm 包
> npm unpublish <package>@<version> # 例: npm unpublish flagwind@1.0.0
刪除發布者
> npm <owner> rm <user> <package> # 例: npm admin rm test flagwind
查看模塊所有者
> npm <owner> ls <package> # 例: npm admin ls flagwind
安裝問題
在 Windows 下直接執行這個命令會遇到一些問題:
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 C:\Python27\python.exe
2、MSBuild 版本不對
MSBUILD : error MSB4132: The tools version "2.0" is unrecognized. Available too ls versions are "4.0"
解決方案: node-gyp 需要用到 Visual C++ Build Tools,百度搜索並安裝即可。
3、Windows下不支持 fs-ext 和 crypt3
node-gyp 報編譯 fs-ext 和 crypt3 失敗的錯誤,安裝 sinopia 時可以忽略,錯誤信息如下:
fs-ext.cc(195): error C3861: 'fcntl': identifier not found [C:\Users\clcaza\AppData\Roaming\npm\node_modules\sinopia\node_modules\.0.6.0@fs-ext\build\fs-ext.vcxproj] crypt3.cc(5): fatal error C1083: Cannot open include file: 'unistd.h': No such file or directory [C:\Users\clcaza\AppData\Roaming\npm\node_modules\sinopia\node\_modules\.0.2.0@crypt3\build\crypt3.vcxproj]
解決方案: sinopia 依賴於 fs-ext
和 crypt3
,但這兩個包是可選的,搜索並刪除 sinopia 安裝目錄下所有帶 fs-ext
和 crypt3
字樣的包
來源:https://www.jianshu.com/p/c2a569be60a9