基于以下理由,企业须要私有的npm仓库。node
然而,架设私有npm仓库并不容易,须要耗费大量精力。最近nodejitsu 开始私有npm仓库托管服务,从$100/月起步,按照托管的包数量收费。git
使用nodejitsu的服务很简单,注册以后修改npm配置便可:github
npm config set registry "http://[your-subdomain].registry.nodejitsu.com"
你能够访问 http://[your-subdomain].registry.nodejitsu.com/manage
设定权限控制。npm
注意,私有仓库中没有的公开的模块,会经过代理的方式访问公开仓库,十分智能。segmentfault
若是你打算自行架设npm私有仓库,能够使用nodejitsu开源出来的smart-private-npm,这样的话你只需将私有的模块放在私有仓库中,公开的模块能够经过代理访问公开仓库。安全
var smartPrivateNpm = require("smart-private-npm"), url = require("url"); // // Configure your private npm. You could load this in from a file // somewhere. // var config = { rewrites: require("./config/rewrites"), proxy: { // // Location of the target public npm registry. // npm: url.parse("http://user:pass@registry.nodejitsu.com"), // // Private npm options. // policy: { npm: url.parse("http://user:pass@private.registry.nodejitsu.com"), private: { // // This is the list of 'known private modules' // that will always be proxied to the private npm. // It is built over time by remembering 'publish' requests. // }, blacklist: { // // This is the list of modules that will ALWAYS be proxies // to the private npm, no matter what. // }, whitelist: { // // If enabled: only requests for these modules will be served // by the proxy (unless they are 'known private modules'). // }, // // In 'transparent mode' the proxy will always forward to // the public registry. // transparent: false } }, // // Server options (from 'create-servers') // http: 80 https: { port: 443, root: "/path/to/your/ssl/files", key: "your-ssl.key", // or .pem key: "your-ssl.cert", // or .pem } }; smartPrivateNpm.createServer(config, function (err, servers) { if (err) { console.log("Error starting private npm: %j", servers); return process.exit(1); } console.log("Private npm running on %j servers.", Object.keys(servers)); });
架设好服务后,能够经过以下方式发布私有模块:less
npm publish some-private-code --reg http://localhost/
除了nodejitsu以外,架设私有仓库还能够考虑阿里开源的cnpm方案。dom
撰文 SegmentFaultui