基于如下理由,企業需要私有的npm倉庫。
- 確定npm服務快速、穩定:對于企業來說,上線生産系統的時候,需要花半小時甚至更久等待npm子產品依賴安裝完畢,是不可接受的。部署鏡像後,可以確定高速、穩定的npm服務。
- 釋出私有子產品:官方的npm上的子產品全部是開源的。一些與企業業務邏輯相關的子產品可能不适合開源。這部分私有的子產品放在私有NPM倉庫中,使用起來各種友善。
- 控制npm子產品品質和安全:npm上的子產品品質參差不齊,搭建私有倉庫,可以更嚴格地控制子產品的品質和安全,隻有經過稽核的子產品才允許被加入私有倉庫。
然而,架設私有npm倉庫并不容易,需要耗費大量精力。最近
nodejitsu開始私有npm倉庫托管服務,從$100/月起步,按照托管的包數量收費。
使用
使用nodejitsu的服務很簡單,注冊之後修改npm配置即可:
npm config set registry "http://[your-subdomain].registry.nodejitsu.com"
你可以通路
http://[your-subdomain].registry.nodejitsu.com/manage設定權限控制。
![](https://img.laitimes.com/img/__Qf2AjLwojIjJCLyojI0JCLicmbw5iM0Y2NwMmY4cDN0QGOwMTO1MzY0MDN5UWYilDMwcDN38CX5d2bs92Yl1iclB3bsVmdlR2LcNWaw9CXt92Yu4GZjlGbh5yYjV3Lc9CX6MHc0RHaiojIsJye.png)
注意,私有倉庫中沒有的公開的子產品,會通過代理的方式通路公開倉庫,十分智能。
smart-private-npm
如果你打算自行架設npm私有倉庫,可以使用nodejitsu開源出來的
,這樣的話你隻需将私有的子產品放在私有倉庫中,公開的子產品可以通過代理通路公開倉庫。
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:[email protected]"),
// Private npm options.
policy: {
npm: url.parse("http://user:[email protected]"),
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));
});
架設好服務後,可以通過如下方式釋出私有子產品:
npm publish some-private-code --reg http://localhost/
除了nodejitsu之外,架設私有倉庫還可以考慮阿裡開源的
cnpm方案。