Skip to content

Commit bfc22f9

Browse files
committed
doc add private npm
1 parent 41c3842 commit bfc22f9

File tree

1 file changed

+151
-0
lines changed

1 file changed

+151
-0
lines changed

doc/private_npm.md

+151
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
# 你一定要知道的private npm
2+
3+
私有模块的好处是必须是模块拥有者才能下载,
4+
5+
## Why?
6+
7+
以后js大一统,npm就不再只是nodejs package manager的,而是js package manager
8+
9+
首先看一下npm的好处
10+
11+
- 易于使用,下载,开发,发布,测试等
12+
- 管理模块依赖
13+
- 模块化,易于拆分
14+
- npm提供足够多的hook,可以定制很多功能
15+
- 安装版本,可随意指定,可以干坏事儿的
16+
17+
私有npm的好处
18+
19+
- 继承了所有npm的好处
20+
- 只有模块拥有者才可以下载,具有私密性
21+
22+
目前情况
23+
24+
- nodejs世界里都是npm,一统江湖
25+
- 前端js世界,正在向npm靠拢,npm的种种好处,只比bower强,不弱。
26+
27+
举个例子,眼下最火的reactjs的组件,推荐就是webpack打包,然后发布都npm上。目前做的比较好的是ant.design,它的tab组件里的package.json
28+
29+
https://github.com/react-component/tabs/blob/master/package.json
30+
31+
我们再看一下它的用法
32+
33+
```
34+
var Tabs = require('rc-tabs');
35+
var TabPane = Tabs.TabPane;
36+
37+
var callback = function(key){
38+
39+
}
40+
41+
React.render(
42+
(
43+
<Tabs defaultActiveKey="2" onChange={callback}>
44+
<TabPane tab='tab 1' key="1">first</TabPane>
45+
<TabPane tab='tab 2' key="2">second</TabPane>
46+
<TabPane tab='tab 3' key="3">third</TabPane>
47+
</Tabs>
48+
),
49+
document.getElementById('t2'));
50+
```
51+
52+
一样的require,一样的调用,是不是感觉很亲切啊?
53+
54+
## Moajs的插件机制:它可以做的更多
55+
56+
核心代码如下
57+
58+
```
59+
"scripts": {
60+
"start": "npm publish .",
61+
"test": "echo \"Error: no test specified\" && exit 1",
62+
"postinstall": "node ./node_modules/nmm-link/link.bin.js"
63+
},
64+
"dependencies": {
65+
"nmm-link": "^1.0.2"
66+
},
67+
```
68+
69+
说明:
70+
71+
- postinstall是npm的hook,当安装完成之后的回调,这里只想nmm-link里的一个脚本
72+
- nmm-link是一个生产软连接的工具
73+
74+
其实整个插件机制异常简单
75+
76+
- 通过npm安装后的回调postinstall来执行脚本
77+
- 根据当前npm目录信息来创建对应的逻辑处理,比如这里的创建link
78+
79+
是不是很简单?
80+
81+
更多参见:https://docs.npmjs.com/misc/scripts
82+
83+
npm supports the "scripts" property of the package.json script, for the following scripts:
84+
85+
- prepublish: Run BEFORE the package is published. (Also run on local npm install without any arguments.)
86+
- publish, postpublish: Run AFTER the package is published.
87+
- preinstall: Run BEFORE the package is installed
88+
- install, postinstall: Run AFTER the package is installed.
89+
- preuninstall, uninstall: Run BEFORE the package is uninstalled.
90+
- postuninstall: Run AFTER the package is uninstalled.
91+
- preversion, version: Run BEFORE bump the package version.
92+
- postversion: Run AFTER bump the package version.
93+
- pretest, test, posttest: Run by the npm test command.
94+
- prestop, stop, poststop: Run by the npm stop command.
95+
- prestart, start, poststart: Run by the npm start command.
96+
- prerestart, restart, postrestart: Run by the npm restart command. Note: npm restart will run the stop and start scripts if no restart script is provided.
97+
98+
这么多hook,是不是想干啥都够?
99+
100+
扯了这么多,我的目的是讲npm的好处,其实这就衍生出私有npm的重要性,很多时候,公司的东西不能开发,就算是一个插件,开发出来也不合适,于是大家都行要私有的npm,于是这帮货就想了个办法,给npm增加scope功能,即所谓的private npm。
101+
102+
## 自建npm源的技术选项
103+
104+
实现private npm有2种办法
105+
106+
- 自建
107+
- 付费购买
108+
109+
110+
我参照的是
111+
112+
https://github.com/cnpm/cnpmjs.org/wiki/Deploy-a-private-npm-registry-in-5-minutes
113+
114+
目前没有成功,有成功的可以分享一下
115+
116+
搭建私有仓库的可选方案
117+
118+
- https://github.com/npm/npm-registry-couchapp
119+
- https://github.com/cnpm/cnpmjs.org
120+
121+
上面2种方式,一个推荐couchdb,一个推荐mysql,总是每个1G内存,甭想玩,而且同步能否限制还不好说。
122+
123+
## 付费方案
124+
125+
npmjs上是7美元/月,可以无限量的上传。私有模块无法被其他源copy,所以要保证源是`registry=https://registry.npmjs.org/`或者`nrm use npmjs`.
126+
127+
破财省得麻烦,也算值得了
128+
129+
## npm私有模块在服务器安装不上?
130+
131+
1) 更新npm版本,必须在2.11以上
132+
133+
npm install -g npm
134+
135+
2) 用户登录,注意是该模块的拥有者
136+
137+
npm login
138+
139+
3) 检查~/.npmrc,是否存在_authToken,如无,请更新npm版本去
140+
141+
```
142+
[deploy@iZ251uvtr2bZ moajs]$ cat ~/.npmrc
143+
registry=https://registry.npmjs.org/
144+
//registry.npmjs.org/:_authToken=fc83b39-7bfa-47b7-9f8c-ed91652613
145+
```
146+
147+
4) 安装你的私有插件吧,下面这句是安装不上的,就是给大家看看
148+
149+
npm install @i5ting/wms-plugin-warehouse
150+
151+

0 commit comments

Comments
 (0)