npm发布自己的npm包及使用

2019-04-19  本文已影响0人  土木三班陈同学_b0a3

[TOC]

###  1.npm 发布自己包到npm平台

1.下载安装node js

注意:发布包的名字不能跟npm网上已有的包重名,不能有大写字母,空格,和下划线

2.创建文件夹,文件夹下面创建readme.md文件(主要介绍你自己的包包含哪些内容),默认的入口文件是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"

}

```

下面例子:文件结构

```

testluhao

lib -> test.js

index.js

package.json

readme.md

```

test.js 文件内容

```

var a = {

fu : function() {

console.log( '这是我的第一个包' );

}

}

module.exports = a;

```

index.js 文件内容

```

var a = require( './lib/test.js' );

module.exports = a;

```

3.打开npm网站注册账号,要邮箱验证一下

在包文件路径下 登陆账号 npm login

4.npm publish

然后就发布成功了,可以在npm平台上面搜到你发布的包了。

如果要撤销发布的包可以用 npm unpublish

如果更新发布后的包1.修改发布包的版本(package.json里面修改version)2.npm publish

###  2.下载并使用自己发布的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搭建私有库

(服务端的ip地址:http://192.168.2.131,使用的时候将http://localhost替换为服端的ip地址)

Sinopia已暂停维护,现由verdaccio在它基础上进行更新升级.

如果你想使用所有npm包系统的好处而且不把代码发送到公共系统里,并且使用私有包像公共一样方便可以使用verdaccio搭建私有库。

1.npm install -g verdaccio  安装verdaccio

2.npm set registry <http://localhost:4873/> npm配置

3.verdaccio 启动

打开http://loalhosy:4873/

### 4.发布自己的本地包

(服务端的ip地址:http://192.168.2.131,使用的时候将http://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

发布成功后,在http://localhost:4873里面打开

###  5.发布成功后,使用刚才发布的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文件

内容:const testluhao = require('testluhao');

testluhao.fu()

node test.js

就可以输出这是我的第一个npm包

因为 之前设置过 npm set registry <http://localhost:4873>,所以当我们去下载npm 官方的包的时候,如果名字一样它会首先在本地去找,如果没有,然后再去npm网站去下载,以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包了。

上一篇 下一篇

猜你喜欢

热点阅读