Exports and Module.exports in node. js

Source: Internet
Author: User

Different programming languages have their own ways of organizing and reusing code, such as. NET, namespaces in PHP, module in Import,ruby in Python, to avoid namespace pollution. I've never figured out the difference between exports and module.exports in node, so we can figure out how node's code module is reused.

First, how to create a modules in node.

You can create a file directly as a module, as follows Module.js

function WriteLine () {    console.log ("Module.js");} Exports.dblenum=function(num) {    return num*2;} Exports.triplenum=function(num) {    return num*3;}

Of these, we exports two functions, in the Example.js file (below), we can use require to get these two functions

var module = require ('./module '); Console.log (Module.dblenum (3)); Console.log ( Module.triplenum (3));

This makes it possible to use the functions in the module.js. Where./indicates that the module and the current calling module are in the same directory, if placed under the subdirectory Lib, is require ('./lib/module '), does not need a suffix. js.

Of course this is a simple single file module, if it is more complex can be in the form of a package, that is, n like a PM installed package that way. As in the Module.js file, we put it under the Example_module folder, Inside a new Package.json file (this is the main message of the package), there is a name, description, dependency, Main, author, version and so on, we briefly do not write so much, mainly wrote the following

{  "name": "Module",  "description": "Example",  "version": "0.0.1",  "dependencies": {    },  " Main ":" Module ",  }

The main file in the General module is Index.js, which is "main": "Index.js". Here the module we write is module.js, I also lazy change, so wrote module.

var module = require ('./example_module '); Console.log (Module.dblenum (3)); Console.log ( Module.triplenum (3));

This makes it possible to invoke the Example_module package. Because it is not installed through NPM, can not directly require (' example_module '); try it yourself, if you create a new Node_modules folder, put Example_module inside, you can directly require (' example _module '), it should be because the NPM installed package will also be installed in the node_modules.

When it comes to the difference between exports and module.exports, it's embarrassing.

If you create a module and want to return one of the variables or functions, it is not possible to assign it directly to exports, as follows

var function () {};calcnum.prototype.dblenum=function(num) {    return num*2;} CalcNum.prototype.tripleNum=function(num) {    return num*3= Calcnum;

This is not possible. But it is possible to replace exports with Module.exports. The calcnum that are poured out at this point, if called in Example.js, will look like this.

var module = require (' module '); var New Module;console.log (Module.dblenum (3)); Console.log (Module.triplenum (3));

This is because exports is a global reference to Module.exports, and Exports.myfunc is the abbreviated notation of Module.exports.myfunc. Then, if any function, object, The variable assigned to exports disconnects the exports from the module.exports, and in fact Module.exports is the true exit, so it is not possible to assign the variable or function directly to exports (exports.myfunc= Myfun is possible because it does not cut off the citation relationship of exports and module.exports). Therefore, in the same module, if exports and module.exports exist simultaneously, exports is ignored.

If there is any mistake, please correct me.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.