创建一个私有的npm库
发布于 1年前 作者 blacktea 1768 次浏览

之前自己搭建了一个npm库,用于放不想公开的模块。但每次部署总是需要执行两条命令来分别安装私有模块和npmjs.org上的模块,于是再搭建了个nginx来把私有库没有的模块代理到npmjs.org上,最终的结果就是安装模块只需要执行一句: npm --registry http://registry.npmjs.local install 为了方便大家和自己下次安装,把脚本贴出来~ 注:搭建环境是Centos 6.3 私有库有个老外写了个教程:《How to create a private npmjs repository》 不过我在Centos上安装与之有些不同,过程如下: 1、下载个比较新的yum源: 更新完epel-release-6-8.noarch.rpm后yum list couchdb了一下, 发现版本是1.0.4-2.el6,但npmjs要求couchdb版本为1.1.0+,没办法只用用源码安装; 2、安装必要的依赖,wiki上有说明: yum install libicu-devel openssl-devel curl-devel make gcc js-devel libtool which gcc-c++ erlang -y 3、编译安装couchdb: ./configure --with-erlang=$erlpath make sudo make install 4、按wiki上说的建立couchdb帐号并给权限: adduser -r --home /usr/local/var/lib/couchdb -M --shell /bin/bash --comment “CouchDB Administrator” couchdb chown -R couchdb:couchdb /usr/local/etc/couchdb chown -R couchdb /usr/local/var/lib/couchdb chown -R couchdb /usr/local/var/run/couchdb chown -R couchdb:couchdb /usr/local/var/log/couchdb chmod -R 0770 /usr/local/etc/couchdb chmod -R 0770 /usr/local/var/lib/couchdb chmod -R 0770 /usr/local/var/log/couchdb chmod -R 0770 /usr/local/var/run/couchdb 5、修改**/usr/local/etc/couchdb/default.ini文件中的bind_address为0.0.0.0,启动服务即可启动couchdb,此时可由地址“http://localhost:5984/_utils/”来访问couchdb,安装couchdb是建私有npm里最复杂的,后面我会贴出脚本来方便大家; 6、按上面老外的教程即可完成私有npm库的搭建,也可见npmjs的github,然后只需将registry指向couchdb即可操作私有npm库: npm --registry http://localhost:5984/registry/_design/scratch/_rewrite login npm --registry http://localhost:5984/registry/_design/scratch/_rewrite publish my-repo 私有npm到这里就建好了,但,链接好长! 于是在/usr/local/etc/couchdb/default.ini文件中添加: [vhosts] example.com = /registry/_design/scratch/_rewrite 这回我们可以用http://localhost:5984来代替那串长长的链接了,接着就是将私有npm库没有的模块(返回404)通过npmjs.org找,于是写nginx配置如下:


    upstream couchdb {
        server 127.0.0.1:5984;
    }
    upstream registry.npmjs.org {
        server registry.npmjs.org;
    }
server {
    listen       80;
    server_name  registry.npmjs.local;
    
    location / {
        #这里一定要带上Host
        proxy_set_header Host registry.npmjs.local:5984;
        proxy_pass http://couchdb;
        proxy_intercept_errors on;
        error_page  404 = [@fallback](/user/fallback);
    }
    
    location [@fallback](/user/fallback) {
        internal;
        proxy_set_header Host registry.npmjs.org;
        proxy_pass http://registry.npmjs.org;
    }
}

至此,我们就能像前面说的只用一条命令即可安装所有依赖的模块了。 最后贴上安装couchdb的脚本:


  mkdir couchdb_install
  cd couchdb_install    
  yum install wget -y    
  wget http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
  wget http://rpms.famillecollet.com/enterprise/remi-release-6.rpm
  rpm -Uvh remi-release-6.rpm epel-release-6-8.noarch.rpm    
  yum install libicu-devel openssl-devel curl-devel make gcc js-devel libtool which gcc-c++ -y
  sleep 2
  yum install erlang -y

name=basename $1 rm -rf $name wget $1

tar -zxvf $name cd ${name/.tar.gz/} if [ "getconf LONG_BIT" = “64” ]; then erlpath=/usr/lib64/erlang/usr/include else erlpath=/usr/lib/erlang/usr/include fi ./configure --with-erlang=$erlpath make make install

adduser -r --home /usr/local/var/lib/couchdb -M --shell /bin/bash --comment “CouchDB Administrator” couchdb chown -R couchdb:couchdb /usr/local/etc/couchdb chown -R couchdb /usr/local/var/lib/couchdb chown -R couchdb /usr/local/var/run/couchdb chown -R couchdb:couchdb /usr/local/var/log/couchdb chmod -R 0770 /usr/local/etc/couchdb chmod -R 0770 /usr/local/var/lib/couchdb chmod -R 0770 /usr/local/var/log/couchdb chmod -R 0770 /usr/local/var/run/couchdb

rm -rf /etc/init.d/couchdb ln -s /usr/local/etc/rc.d/couchdb /etc/init.d/couchdb chkconfig --add couchdb chkconfig --level 1234 couchdb on

conf=cat /usr/local/etc/couchdb/default.ini | sed -e 's/bind_address = 127.0.0.1/bind_address = 0.0.0.0/g' echo “$conf” > /usr/local/etc/couchdb/default.ini

service couchdb restart

执行: sudo sh couchdb-install http://mirrors.tuna.tsinghua.edu.cn/apache/couchdb/source/1.3.1/apache-couchdb-1.3.1.tar.gz

3 回复

有用,谢谢

好分享。收藏了

回到顶部