ndoe.js 和npm私有倉庫的搭建

下載nodejs的壓縮包 網址:https://nodejs.org/en/ node

下載以tar.xz結尾的包例如:node-v8.9.4-linux-x64.tar.xzlinux

上傳包到制定的目錄 能夠用lrzszwebpack

解壓:xz -dnginx

 node-v8.9.4-linux-x64.tar.xzgit

而後在 拆包: tar –xvf node-v8.9.4-linux-x64.targithub

配置環境變量在/etc/profileweb

Vim /etc/profilr  最後加入node的變量npm

export NODE=/root/node-v8.9.4-linux-x64  #node的安裝路徑json

export PATH=$PATH:$NODE/bin          #node的bin目錄vim

推出聲明一下

Source  /etc/profile    #環境變量就設好了

查看是否安裝成功:出現版本號就是安裝成功了

 

配置npm的源:

npm set registry https://registry.npm.taobao.org/ # 推薦淘寶npm鏡像

安裝配置sinopia

npm install -g sinopia

啓動sinopia

輸入: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

# path to a directory with all packages

storage: /home/{user}/.local/share/sinopia/storage # 庫存路徑,須要考慮磁盤空間

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 # 添加用戶(npm adduser)後自動建立,保存用戶信息,能夠初始化用戶

    # Maximum amount of users allowed to register, defaults to "+inf".

    # You can set this to -1 to disable registration.

    #max_users: 1000 # 設置爲-1不能npm adduser

# a list of other known repositories we can talk to

uplinks: # 能夠配置多個上游地址,後面packages中的proxy指定用哪一個

  npmjs:

    url: https://registry.npm.taobao.org/ # 更改此上游地址

    # amount of time to wait for repository to respond

    # before giving up and use the local cached copy

    #timeout: 30s # 請求上游地址超時時間

    # 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 # 包過時時間

    # if two subsequent requests fail, no further requests will be sent to

    # this uplink for five minutes

    #max_fails: 2 # 允許依賴請求最大失敗數

    #fail_timeout: 5m # 依賴請求超時時間

packages: # 包的權限管理,$all爲全部人,$authenticated爲經過驗證人

                  # 分佈和安裝兩種權限,值能夠特指某幾人

  '@*/*': # 跟package.json中的name屬性進行匹配

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

 

listen: 0.0.0.0:4873 # 設置監聽地址,0.0.0.0匹配本機地址

 

# if you use nginx with custom path, use this to override links

#url_prefix: https://dev.company.local/sinopia/

# Configure HTTPS, it is required if you use "https" protocol above.

#https:

#  key: path/to/server.key

#  cert: path/to/server.crt

# 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

# maximum size of uploaded json document

# increase it if you have "request entity too large" errors

#max_body_size: 1mb # http請求body大小

修改uplinks及listen值,同上,重啓sinopia
若是想引用別的配置文件,請經過sinopia -c <配置文件>指定

 

 

用pm2託管sinopia

上面方式啓動sinopia只是暫時的,退出命令行就沒有了,所以須要一個長期開啓sinopia方案,經過pm2託管,可讓sinopia進程永遠活着,就算意外掛了也可自動重啓。
安裝pm2

$ npm install -g pm2

安裝完後,使用pm2啓動sinopia

$ pm2 start sinopia

關閉時

pm2 stop sinopia

最後更改npm源地址爲私有庫地址

http:/{服務器ip}:4873/# 內網測試可行$ npm set registry/

 

 

Nrm是npm的管理工具:

nrm的安裝:

添加私有倉庫

nrm add mynpm http://192.168.44.139:4873

 

使用私有倉庫

nrm use mynpm

 

測試私有倉庫

$ mkdir test
$ cd test
# 第一次安裝比較慢$ npm install webpack
...
 
$ rm -rf webpack
# 第二次安裝就比較快了




$ npm install webpack

搭建好私有npm倉庫後遇到什麼坑?

我發現使用nrm切換到私有npm倉庫對應的源後,下載帶@ 符號的包都下載失敗,好比下載 @angular/core,就會下載失敗,這是爲何呢,查閱了一些資料,發現這實際上是Sinopia本身的bug,bug產生的緣由就是:sinopia在代理到npmjs.org公有庫時將@符號轉碼爲%40,導致在公有庫中找不到對應的包,返回404 ,簡單點說就是 @angular/core 代理請求的時候被轉換成了 %40angular/core,因此咱們須要在代理請求發出以前將其轉回 @angular/core

 

修改sinopia源碼:修改位於sinopia/lib/up-storage.js文件第10行:將var encode = encodeURIComponen;,更改成:var encode = function(thing) {return encodeURIComponent(thing).replace(/^%40/, '@');}; ,這段代碼的含義就是將%40轉回@,因而就解決了不能下載帶有@符號的npm包的bug

 

如今sinopia很差用有時解決不了下載帶@ 符號的包都下載失敗的問題 ,就不須要安裝sinopia能夠安裝更好的verdaccio

 

安裝 verdaccio

npm  install –g verdaccio

安裝完之後能夠啓動 

verdaccio 例:

 

會顯示撇子文件的路徑等等,可是這個是其餘運行,退出終端就掛,因此要安裝mp2 來管理verdaccio

 

  修改配置文件Verdaccio

cd  /root/.config/verdaccio

vim  config.yaml  (實例)(也能夠參考https://verdaccio.org/docs/en/configuration)

 

 

 

(2)安裝pm2進程管理 並啓動verdaccio服務

    •npm install –g pm2

    •ls –s/root/node-v6.9.5-linux-x64/bin/pm2 /usr/local/bin/pm2  (這不能夠不作)

    •pm2 start `which verdaccio`   #啓動

 



 

 

 

 





 網上的資料散 搞了好半天,總算完成了  哈哈哈哈

 

能夠參考  https://segmentfault.com/a/1190000005790827

相關文章
相關標籤/搜索