---
title: "NodeJS Module Quick Reference"
author: "fairy_xzc"
date: "2017-01-19T15:58:36Z"
tags: []
categories: ["Web前端"]
slug: "nodejs-module-quick-reference"
draft: false
lastmod: "2017-03-14T10:10:44Z"
id: 2354063
url: "https://agilestyle.iteye.com/blog/2354063"
---

**原创转载请注明出处：[http://agilestyle.iteye.com/blog/2354063](/blog/2354063)**

 

## Project Directory

![](http://dl2.iteye.com/upload/attachment/0122/7048/27ca7342-1a16-37d4-9f3f-6c40d7501041.png)  
 

## exports.xxx = function() {}

hello.js

```
exports.world = function() {
	console.log("Hello World");
};
```

 

## module.exports = xxx

world.js

```
function Person(name) {
	this.name = name;
}

Person.prototype = {
	constructor: Person,

	sayName: function() {
		console.log(this.name);
	},

	toString: function() {
		return "[Person " + this.name + "]";
	}
};

module.exports = Person;
```

 

index.js

```
// exports.world
var hello = require('./hello');
hello.world();

// module.exports
var world = require('./world');
person = new world();
person.sayName();

person = new world('nodejs');
person.sayName();
```

 

## Run

![](http://dl2.iteye.com/upload/attachment/0122/7050/83ae8b97-fe1e-3868-baa3-ee6fa5034670.png)  
 

## Conclusion

**外部引用exports.xxx = function() {}和module.exports = xxx模块接口的唯一区别就是后者需要new一下。**

 

## Reference

更加详细的解释可以参考

[exports 和 module.exports 的区别](https://cnodejs.org/topic/5231a630101e574521e45ef8)