Skip to content

Latest commit

 

History

History
24 lines (17 loc) · 766 Bytes

02modules.md

File metadata and controls

24 lines (17 loc) · 766 Bytes

#Node Modules

Importing and Exporting Modules with Node

Node's module system allows code written in a partiuclar file (or folder) to be exported, and then imported into other files. Here's an example of a module called Person.js being imported into a file.

Person.js

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

module.exports = Person;

app.js

var Person = require('./Person.js');

var brian = new Person('Brian');

To view a practical example of importing and exporting modules, read this article. You'll see that we can export multiple functions by assigning module.exports to an object. This is a pattern that we'll see frequently in Node.