module.exports, exports modular import and export methods

This article is transferred from: https://www.cnblogs.com/shichangchun/p/10495987.html

In the process of modular development, we often encounter the export and import of modules. Involve   require, export, module.exports and exports. But what's the difference. I really often get confused.

Just take care of yourself.

First of all, generally distinguish between the two norms   CommonJS module specification and ES6 module specification

Require: both node and es6 support the introduction of
export / import: only es6 supports export import
Module.exports/exports: only node supported exports

The module system in Node follows the CommonJS specification.
Modules defined by CommonJS are divided into: module ID (module), module definitions (exports), and module reference (require)
 
===================CommonJS module specification   module.exports / exports / require===========================

For convenience, Node provides an exports variable for each module, pointing to module.exports. This is the same as the header of each module. There is one such command.

var exports = module.exports;

Refer to the test code for details

New utils.js
console.log('1=', module.exports); // The result is: {}
console.log('2=', exports); // The result is: {}

exports.a = 200; // Since the default exports=module.exports, the value of module.exports is also changed {A: 200}

console.log('3=', module.exports) // {a : 200}
console.log('4=', exports) // {a : 200}

exports = 'I'm not pointing module'

console.log('5=', module.exports) // {a : 200}
console.log('6=', exports) // I'm not pointing to module

Create test.js and import utils.js

var util = require('./utils')

console.log('test=', util) // { a: 200 }
As can be seen from the above, the content exported by require is the memory block content pointed to by module.exports, not exports.
In short, the difference between them is that exports is only a reference to module.exports, which is used to assist the latter in adding content.
Many suggestions are to export with module.exports and then import with require.
 
Sample code
hello.js
!function(){
  module.exports = function(name,age,money) {
    this.name = name;
    this.age = age;
    this.money = money;
    this.say = function() {
      console.log('My name is:'+this.name+',I this year'+this.age+'Years old, monthly salary:'+this.money+'Yuan;')
    }
  };
}()

hello2.js

module.exports = function(name,age,money) {
  this.name = name;
  this.age = age;
  this.money = money;
  this.say = function() {
    console.log('My name is:'+this.name+',I this year'+this.age+'Years old, monthly salary:'+this.money+'Yuan;')
  }
};

hello3.js

function sayDog () {
  console.log('i am dog')
}

function sayCat () {
  console.log('i am cat')
}

exports.sayDog = sayDog
exports.sayCat = sayCat

index.js

var Hello = require('./hello');
var hello = new Hello('jone','28','10000')
hello.say(); 

var Hello2 = require('./hello2');
var hello2 = new Hello2('hello2','28','10000')
hello2.say(); 

var h3 = require('./hello3')
h3.sayDog()
h3.sayCat()

The input results are as follows

 

 =================== ES6 specification   import / export / export default ===========================
Unlike CommonJS, ES6 uses export and import to export and import modules.
Use scenarios such as api encapsulation in our project.
/*
 * Help document query api
 * @Author: shichangchun
 * @Date: 2018 August 30, 2014 14:22:15
 */
import fetch from '@/util/fetch'

/**
 * Query article add October 19, 2018
 */
export const getArticleByPath = (data) => {
  return fetch({
    url: '/api/help/contentTxtByPath',
    method: 'GET',
    params: data
  })
}

/**
 * Query directory add October 19, 2018
 */
export const getMuneByPath = (data) => {
  return fetch({
    url: '/api/help/contentNewByPath',
    method: 'GET',
    params: data
  })
}

/**
 * Home page query
 * @param data
 */
export const getHelpList = (data) => {
  return fetch({
    url: '/api/help/catalogByPath',
    method: 'GET',
    params: data
  })
}

Reference in component

It should be noted that the export command specifies the external interface and must establish a one-to-one correspondence with the variables inside the module, that is, import {'corresponding name 1', 'corresponding name 2'} from 'api'.
Use the export default command to specify the default output for the module.
In addition:
##Both export and export default can be used to export constants, functions, files, modules, etc
##In a file or module, there can be multiple exports and import s, and only one export default
##When exporting through export, {} should be added during import, and export default is not required
##Export can directly export variable expressions, but export default cannot.
reference resources: http://javascript.ruanyifeng.com/nodejs/module.html
   http://es6.ruanyifeng.com/#docs/module
   https://www.cnblogs.com/fayin/p/6831071.html

Posted on Sat, 04 Dec 2021 13:59:39 -0500 by phpmania1