Node.js模块

Node.js 模块和 Node.js 包介绍。

一、Node.js模块

每一个Node.js都是一个Node.js模块,包括JavaScript文件(.js)、JSON文本文件(.json)和二进制模块文件(.node)。

1. 模块的使用

编写一个模块:在虚拟机桌面新建一个文件mymodule.js,输入如下代码并保存:

<pre>
function hello() {
console.log('Hello');
}
function world() {
console.log('World');
}
</pre>
这就是一个Node.js模块,但是怎么在其他模块中引入并使用这个模块呢?我们需要为模块提供对外的接口,这就要用到module.exports和exports

*我们可以这样写mymodul.js:
<pre>
function hello() {
console.log('Hello');
}
function world() {
console.log('World');
}
exports.hello = hello;
exports.world = world;
</pre>
*在其他模块中,可以使用require(module_name);载入需要的模块,如,在虚拟机桌面新建index.js
,输入如下代码并保存:
<pre>
var hello = require('./mymodule'); // 也可以写作
var hello = require('./mymodule.js');// 现在就可以使用mymodule.js中的函数了
hello.hello(); // >> Hello
hello.world(); // >> World
</pre>

也可以这样写mymodule.js
<pre>
function Hello() {
this.hello = function() {
console.log('Hello');
};
this.world = function() {
console.log('World');
};
}
module.exports = Hello;
</pre>

此时,index.js
就要改成这样:
<pre>
var Hello = require('./mymodule');
var hello = new Hello();
hello.hello(); // >> Hello
hello.world(); // >> World
</pre>

2. module.exports和exports

module是一个对象,每个模块中都有一个module对象,module是当前模块的一个引用。
module.exports对象是Module系统创建的,而exports可以看作是对module.exports对象的一个引用。
在模块中require另一个模块时,以module.exports的值为准,
因为有的情况下,module.exports和exports它们的值是不同的。
module.exports和exports的关系可以表示成这样:

<pre>
// module.exports和exports相同的情况
var m = {}; // 表示 module
var e = m.e = {}; // e 表示 exports, m.e 表示 module.exports
m.e.a = 5;
e.b = 6;
console.log(m.e); // Object { a: 5, b: 6 }
console.log(e); // Object { a: 5, b: 6 }
</pre>

二、Node.js包

1. 包

包用于管理多个模块及其依赖关系,可以对多个模块进行封装,
包的根目录必须包含package.json文件,
package.json文件是CommonJS规范用于描述包的文件,
符合CommonJS规范的package.json文件应该包含以下字段:

  • name:包名。包名是唯一的,只能包含小写字母、数字和下划线。
  • version:包版本号。
  • description:包说明。
  • keywords:关键字数组。用于搜索。
  • homepage:项目主页。
  • bugs:提交bug的地址。
  • license:许可证。
  • maintainers:维护者数组。
  • contributors:贡献者数组。
  • repositories:项目仓库托管地址数组。
  • dependencies:包依赖。
    下面是一个package.json示例:
    <pre>
    {
    "name": "shiyanlou",
    "description": "Shiyanlou test package.",
    "version": "0.1.0",
    "keywords": [
    "shiyanlou",
    "nodejs"
    ],
    "maintainers": [{
    "name": "test",
    "email": "test@shiyanlou.com"
    }],
    "contributors": [{
    "name": "test",
    "web": "http://www.shiyanlou.com/"
    }],
    "bugs": {
    "mail": "test@shiyanlou.com",
    "web": "http://www.shiyanlou.com/"
    },
    "licenses": [{
    "type": "Apache License v2",
    "url": "http://www.apache.org/licenses/apache2.html"
    }],
    "repositories": [{
    "type": "git",
    "url": "http://github.com/test/test.git"
    }],
    "dependencies": {
    "webkit": "1.2",
    "ssl": {
    "gnutls": ["1.0", "2.0"],
    "openssl": "0.9.8"
    }
    }
    }
    </pre>
2. npm包管理工具

由于实验楼环境网络限制,所以npm命令会连接taobao的源,而不会直接连接官方源。
npm 也可以从第三方网站(http://www.npmjs.org/)上下载第三方Node.js包。

在实验楼的环境中搜索包(默认会连接到taobao的Node.js源):

  • shiyanlou@cqqg0heZ:~$ sudo npm search express

  • 安装包:
    shiyanlou@cqqg0heZ:~$ sudo npm install -g express

  • 更新包:
    shiyanlou@cqqg0heZ:~$ sudo npm update express

  • 卸载包:
    shiyanlou@cqqg0heZ:~$ sudo npm uninstall express

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容