[TOC]
###? 1.npm 發(fā)布自己包到npm平臺
1.下載安裝node js
注意:發(fā)布包的名字不能跟npm網(wǎng)上已有的包重名咒吐,不能有大寫字母矮瘟,空格,和下劃線
2.創(chuàng)建文件夾搜吧,文件夾下面創(chuàng)建readme.md文件(主要介紹你自己的包包含哪些內(nèi)容),默認(rèn)的入口文件是index.js盲赊,在里面把要用的方法暴露出去。
npm init 配置package.json文件如下:
```
package name: (testlaowang) testluhao
version: (1.0.0)
description:
entry point: (index.js)
test command:
git repository:
keywords:
author: luhao
license: (ISC)
About to write to /Users/luhao/Desktop/testlaowang/package.json:
{
? "name": "testluhao",
? "version": "1.0.0",
? "description": "",
? "main": "index.js",
? "scripts": {
? ? "test": "echo \"Error: no test specified\" && exit 1"
? },
? "author": "luhao",
? "license": "ISC"
}
```
下面例子:文件結(jié)構(gòu)
```
testluhao
lib -> test.js
index.js
package.json
readme.md
```
test.js 文件內(nèi)容
```
var a = {
fu : function() {
console.log( '這是我的第一個包' );
}
}
module.exports = a;
```
index.js 文件內(nèi)容
```
var a = require( './lib/test.js' );
module.exports = a;
```
3.打開npm網(wǎng)站注冊賬號惊窖,要郵箱驗證一下
在包文件路徑下 登陸賬號 npm login
4.npm publish
然后就發(fā)布成功了刽宪,可以在npm平臺上面搜到你發(fā)布的包了。
如果要撤銷發(fā)布的包可以用 npm unpublish
如果更新發(fā)布后的包1.修改發(fā)布包的版本(package.json里面修改version)2.npm publish
###? 2.下載并使用自己發(fā)布的npm包
新建test文件夾
npm install testluhao
package.json如下圖所示
```
{
? "name": "test",
? "version": "1.0.0",
? "description": "",
? "main": "index.js",
? "scripts": {
? ? "test": "echo \"Error: no test specified\" && exit 1"
? },
? "author": "",
? "license": "ISC",
? "dependencies": {
? ? "testluhao": "^1.0.1"
? }
}
```
新建test.js文件
```
var testluhao = require( "testluhao" );
testluhao.fu();
```
node test.js
就可以輸出這是我的第一個npm包
### 3.用verdaccio搭建私有庫
(服務(wù)端的ip地址:http://192.168.2.131界酒,使用的時候?qū)ttp://localhost替換為服端的ip地址)
Sinopia已暫停維護(hù)圣拄,現(xiàn)由verdaccio在它基礎(chǔ)上進(jìn)行更新升級.
如果你想使用所有npm包系統(tǒng)的好處而且不把代碼發(fā)送到公共系統(tǒng)里,并且使用私有包像公共一樣方便可以使用verdaccio搭建私有庫毁欣。
1.npm install -g verdaccio? 安裝verdaccio
2.npm set registry <http://localhost:4873/> npm配置
3.verdaccio 啟動
打開http://loalhosy:4873/
### 4.發(fā)布自己的本地包
(服務(wù)端的ip地址:http://192.168.2.131庇谆,使用的時候?qū)ttp://localhost替換為服端的ip地址)
1.使用 npm set registry <http://localhost:4873>
2.npm adduser —- registry <http://localhost:4873>
如下所示:
username: luhao
Password:
Email: (this IS public) 511496772@qq.com
Logged in as luhao on <http://localhost:4873/>.
添加賬戶成功
3.npm publish
發(fā)布成功后岳掐,在http://localhost:4873里面打開
###? 5.發(fā)布成功后,使用剛才發(fā)布的npm包
以testluhao為例
新建test文件夾
npm install testluhao
package.json
```
{
? "name": "npmtest2",
? "version": "1.0.0",
? "description": "",
? "main": "index.js",
? "scripts": {
? ? "test": "echo \"Error: no test specified\" && exit 1"
? },
? "author": "",
? "license": "ISC",
? "dependencies": {
? ? "testluhao": "^1.0.1"
? }
}
```
新建test.js文件
內(nèi)容:const testluhao = require('testluhao');
testluhao.fu()
node test.js
就可以輸出這是我的第一個npm包
因為 之前設(shè)置過 npm set registry <http://localhost:4873>饭耳,所以當(dāng)我們?nèi)ハ螺dnpm 官方的包的時候串述,如果名字一樣它會首先在本地去找,如果沒有哥攘,然后再去npm網(wǎng)站去下載剖煌,以express為例:
mkdir npmtest2
npm init
npm install express
```
npm WARN npmtest2@1.0.0 No description
npm WARN npmtest2@1.0.0 No repository field.
+ express@4.16.4
added 48 packages from 36 contributors and audited 123 packages in 17.363s
found 0 vulnerabilities
```
然后就可以在node_modules里面看到express包含的npm包了。