windows 環境部署私有 npm 源

sinopia 是一個零配置帶緩存功能的 npm 包管理工具。

sinopia 有如下幾個優點值得關注:php

  1. 不一樣步拉取 npm 庫,佔據大量硬盤,沒有硬盤被撐爆的問題;
  2. 安裝配置極其簡單,不須要數據庫;
  3. 支持配置上游 registry 配置,一次拉取即緩存;
  4. 支持 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/ 若是能正常顯示,說明安裝成功。node

 
 

sinopia 啓動時默認會建立 config.yaml 文件,文件路徑能夠看輸出的提示。咱們將上面路徑的 config.yaml 拷貝到指定盤符的文件夾下,使用 -c 選項指定配置文件,並執行命令:python

> sinopia -c D:\sinopia\config.yaml

如今咱們就能夠在指定目錄下運行了,之後上傳的 npm 包也會放在這個目錄中。git

配置

config.yaml 是用來配置訪問權限,代理,文件存儲路徑等全部配置信息的:github

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

  1. 須要安裝私有 npm 包時,請執行 nrm use sinopia 切換到私有 npm 源
  2. 安裝公有 npm 包時,請執行 nrm use taobao 切換到淘寶源安裝

nrm 其餘命令:緩存

> nrm --help # 查看nrm命令幫助 > nrm list # 列出可用的 npm 鏡像地址 > nrm use taobao # 使用`淘寶npm`鏡像地址 

經常使用 npm 命令

註冊發佈者ruby

> npm adduser                        # 按提示輸入用戶名,密碼,郵箱便可 

登錄 npm 源bash

> 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 下直接執行這個命令會遇到一些問題:

一、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 

二、MSBuild 版本不對

MSBUILD : error MSB4132: The tools version "2.0" is unrecognized. Available too ls versions are "4.0" 

解決方案: node-gyp 須要用到 Visual C++ Build Tools,百度搜索並安裝便可。

三、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-extcrypt3,但這兩個包是可選的,搜索並刪除 sinopia 安裝目錄下全部帶 fs-extcrypt3 字樣的包

來源:https://www.jianshu.com/p/c2a569be60a9

相關文章
相關標籤/搜索