精华 5分钟搭建一个私有 npm registry
发布于 10 年前 作者 fengmk2 17601 次浏览 最后一次编辑是 8 年前 来自 分享

持续更新: https://github.com/cnpm/cnpmjs.org/wiki/Deploy-a-private-npm-registry-in-5-minutes

Install cnpmjs.org and cnpm from npm

$ npm install -g --build-from-source cnpmjs.org cnpm sqlite3

If you’re in China, maybe you should use China mirror

$ npm install -g --build-from-source \
  --registry=http://registry.npm.taobao.org \
  --disturl=http://npm.taobao.org/mirrors/node
  cnpmjs.org cnpm sqlite3

Start cnpmjs.org server with configs

  • admins: myname,othername
  • scopes: my-company-name,other-name
  • default ports: 7001-registry, 7002-web
$ nohup cnpmjs.org start --admins='myname,othername' --scopes='@my-company-name,@other-name' &

Change cnpm default registry to your private registry

$ cnpm set registry http://localhost:7001

Use cnpm to login yourself on your private registry

$ cnpm login
Username: myname
Password: ***
Email: (this IS public) m@fengmk2.com

Publish your private package now!

Private package should be a scoped package

$ cd /tmp
$ mkdir helloworld && cd helloworld
$ cnpm init
name: (helloworld) @my-company-name/helloworld
version: (1.0.0)

{
  "name": "@my-compny-name/helloworld",
  "version": "1.0.0",
  "description": "my first scoped package",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC"
}

$ cnpm publish
+ @my-company-name/helloworld@1.0.0

View your private package

You can visit with bowsers

$ open http://localhost:7002/@my-company-name/helloworld

Or use cnpm info

$ cnpm info

All public packages available directly

You can install all public packages from npmjs.org

$ cnpm install mocha
16 回复

亲,node modules在本地安装次数太多了,有更好的办法么?

-g 不算

@i5ting 没太看明白,是说依赖太多了?

@fengmk2 不是这个意思 ,是说重复依赖依赖

比如一个项目基于express,他会有debug,cookie等

但是再express-session里也有,这样的重复太不经济了

想想确实是很重复.

@i5ting @fengmk2 应该是说,多个项目使用相同的module,但是每次都安装么?环境一样,直接cp算不算,我看npm也有缓存,装一下不费事 >.<

@i5ting @fengmk2 哦,理解错误~那样是感觉不爽~

@alsotang 页面几十分钟没刷,没看到LZ的回复,想当然的回复了错误的理解。要是在评论有增加时,自动推送就好了。

@i5ting 这个重复依赖…其实…也没啥…就耗一点磁盘空间…可以省下很多麻烦的…

现在整个npm 需要多大磁盘空间?

npm 3 将会在 npm install 的时候进行 npm dedupe 类似的功能,详细请看 http://blog.npmjs.org/post/109921886740/a-belated-npm-weekly-3

@karlsun 推送android可以 iOS依然审核中 目测这周能上线 LOL 自豪地采用 CNodeJS ionic

@i5ting 如果 express 和 express-session 依赖的 debug 版本号能够被合并,只会安装一个版本的 debug

@fengmk2 这个是

我跑了个脚本,查询package.json

我的workspace里竟然有 20160 个。。。。。。。太恐怖了

其实我更想的探寻一种可能,本地起一个local registry,这个会节省很多网络资源,没有人希望每次都等

@fengmk2 本地没有部署成功啊,又没有报错

cnpm login 的时候失败了?这个是需要到npm服务器上验证的嘛?

[root@iZ28qt5vcnzZ ~]# cnpm login Username: ganxz Password: Email: (this IS public) ganxunzou@163.com | | npm ERR! Linux 3.10.0-123.9.3.el7.x86_64 npm ERR! argv “/root/.nvm/v5.7.1/bin/node” “/root/.nvm/v5.7.1/lib/node_modules/cnpm/node_modules/.bin/npm” “–userconfig=/root/.cnpmrc” “–disturl=https://npm.taobao.org/mirrors/node” “–cache=/root/.cnpm” “–registry=http://localhost:7001” “–node-gyp=/root/.nvm/v5.7.1/lib/node_modules/cnpm/node_modules/node-gyp/bin/node-gyp.js” "login" npm ERR! node v5.7.1 npm ERR! npm v2.14.22 npm ERR! code ECONNREFUSED npm ERR! errno ECONNREFUSED npm ERR! syscall connect

npm ERR! Error: connect ECONNREFUSED 127.0.0.1:7001 npm ERR! at Object.exports._errnoException (util.js:856:11) npm ERR! at exports._exceptionWithHostPort (util.js:879:20) npm ERR! at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1057:14) npm ERR! { [Error: connect ECONNREFUSED 127.0.0.1:7001] npm ERR! code: ‘ECONNREFUSED’, npm ERR! errno: ‘ECONNREFUSED’, npm ERR! syscall: ‘connect’, npm ERR! address: ‘127.0.0.1’, npm ERR! port: 7001 } npm ERR! npm ERR! If you are behind a proxy, please make sure that the npm ERR! ‘proxy’ config is set properly. See: ‘npm help config’

npm ERR! Please include the following file with any support request: npm ERR! /root/npm-debug.log [root@iZ28qt5vcnzZ ~]#

回到顶部