基於以下理由,企業須要私有的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