位置:电子教程 > Node.js在线教程 (如果看不到内容请使用360浏览器) 推荐学习资源
Node概要
Node.js 安装配置
安装开发工具vscode和配置汉化
Node.js 创建第一个应用
NPM 使用介绍
Node.js REPL(交互式解释器)
Node.js 回调函数
Node.js 事件循环
Node.js EventEmitter
Node.js Stream(流)
Node.js模块系统
Node.js 全局对象
console
Node.js 函数
Node.js 路由
Node.js 常用工具
Node.js GET/POST请求
当前阅读教程:Node.js在线教程 > 创建模块
阅读(22555525)      收藏       赞(5685)      分享
上一篇: 模块操作 下一篇: 创建模块

创建模块,package.json 文件是必不可少的。我们可以使用 NPM 生成 package.json 文件,生成的文件包含了基本的结果。

$ npm init

This utility will walk you through creating a package.json file.

It only covers the most common items, and tries to guess sensible defaults.

 

See `npm help json` for definitive documentation on these fields

and exactly what they do.

 

Use `npm install <pkg> --save` afterwards to install a package and

save it as a dependency in the package.json file.

 

Press ^C at any time to quit.

name: (node_modules) runoob                   # 模块名

version: (1.0.0)

description: Node.js 测试模块(www.runoob.com)  # 描述

entry point: (index.js)

test command: make test

git repository: https://github.com/runoob/runoob.git  # Github 地址

keywords:

author:

license: (ISC)

About to write to ……/node_modules/package.json:      # 生成地址

 

{

  "name": "runoob",

  "version": "1.0.0",

  "description": "Node.js 测试模块(www.runoob.com)",

  ……

}

 

 

Is this ok? (yes) yes

以上的信息,你需要根据你自己的情况输入。在最后输入 "yes" 后会生成 package.json 文件。

接下来我们可以使用以下命令在 npm 资源库中注册用户(使用邮箱注册):

$ npm adduser

Username: mcmohd

Password:

Email: (this IS public) mcmohd@gmail.com

接下来我们就用以下命令来发布模块:

$ npm publish

如果你以上的步骤都操作正确,你就可以跟其他模块一样使用 npm 来安装。

 

版本号

使用NPM下载和发布代码时都会接触到版本号。NPM使用语义版本号来管理代码,这里简单介绍一下。

语义版本号分为X.Y.Z三位,分别代表主版本号、次版本号和补丁版本号。当代码变更时,版本号按以下原则更新。

Ø  如果只是修复bug,需要更新Z位。

Ø  如果是新增了功能,但是向下兼容,需要更新Y位。

Ø  如果有大变动,向下不兼容,需要更新X位。

版本号有了这个保证后,在申明第三方包依赖时,除了可依赖于一个固定版本号外,还可依赖于某个范围的版本号。例如"argv": "0.0.x"表示依赖于0.0.x系列的最新版argv


上一篇: 模块操作 下一篇: 创建模块
计算机毕业设计作品网      毕业设计文档网      小程序教程网       毕业设计资料网  |         毕业设计定制QQ:45157718(微信同号)(备注:毕设)