nodejs Foundation: reference module instances and exports and module Exports differences

As mentioned earlier, the birth of modules in JavaScript is later than the module concept of nodejs. Of course, the imported data of nodejs is also formatted. The keywords export and import are used in ES6, and the key keywords in nodejs are exports and require:

  • exports: exposes the js data in the module (usually a separate js by default). But some write module exports. The specific similarities and differences between the two are also discussed below.
  • require: import the data in the module into the current module. require when referencing a module, the module code will be executed automatically.

Modules in nodejs come from different sources and are generally divided into three types:

  • Built in modules: These are built-in modules provided by nodejs, which can be directly referenced, such as fs, path, http, etc
  • Custom module: a module created by the programmer
  • Third party module: a module written by a third party. It is neither built-in nor written by the user. It is equivalent to the reference of the third package in java. However, this module needs to be downloaded locally and then used. (I will talk about npm when talking about this.)

Of course, there are some nouns that need to be understood briefly:

  • Module scope:

    This means that this variable is controlled by the effect in the js, not a window variable, and the imported file cannot directly reference the referenced variable.

  • module object:

    Each imported js custom module has a module object, which stores all the information of the current module

The old rule demonstrates one:

Port command running nodejs

Premise: install nodejs, the js running environment.

Let's start with a test JS

console.log('nodejs helloworld');

Call this command from the command window

# The first step is to open the port and then cd to the file where js is located
cd  .....js

Or directly enter: cmd in the js file and then in the address bar

Then enter:

node ./test.js

However, for the convenience of not using windows, nodejs is integrated into the programming software, such as pytcharm, which I use, and then the nodejs plug-in is installed. So it can be run directly. The following is also demonstrated through pycharm, not through port, but the js shown can also be run through port.

Module object

First, let's take a look at what the module object is?

console.log(module);

  • id: this output is
  • Path: the file path where nodejs runs the js module.
  • exports: This is the object exposed by the module, that is, the reference js module can be used in the reference file.
  • filename: current module file name with path.
  • Loaded: the attribute value is a Boolean value. When the value is false, the module has not been loaded. When the attribute value is true, the module has been loaded.
  • Children: an array containing the information of the reference module in the module (for example, b refers to a, so the data of children in the module in b includes id, path and other data).
  • paths: is the folder used to store the packages downloaded and installed by the package management tool after installing node. Call the packages in the same directory first. If they cannot be found, find the parent path, and then go to the root path to form an array.

Module reference

Example demonstration

Now let's take a module reference demonstration:

First file: test JS

console.log('test Automatically executed when referenced')
var name='wolf';
exports.name= name;
exports.drive=function(){
    console.log('Drive a car');
}

Second file: Test_ Main JS

// When importing, it is usually named after the called module for easy use
const test= require('./test.js');
// Variables that call a module cannot be used directly
// Console Log ('not directly callable - ', name) //name is not defined
console.log('By returning module Object call-----',test.name)
test.drive();

// The children attribute will be demonstrated later
console.log(module.children)

Then look at the call:

Since exports is an attribute of module, it can also be written as follows:

console.log('test Automatically executed when referenced')
var name='wolf';
module.exports.name= name;
module.exports.drive=function(){
    console.log('Drive a car');
}

Finally, the result is the same, so the result image is no longer pasted.

The two seem to be the same thing, and then try the following:

Test JS is changed as follows:

console.log('test Automatically executed when referenced')
var name='wolf';
exports.name= name;
module.exports.drive=function(){
    console.log('Drive a car');
}

Test_ Main JS is as follows:

// When importing, it is usually named after the called module for easy use
const test= require('./test.js');
console.log('By returning module Object call-----',test.name)
test.drive();

Module Differences between export and export

The above demonstration looks like exports and module Exports all point to an object.

It can be demonstrated as follows:

console.log('typeof exports:   ',typeof exports);
console.log('typeof module.exports:  ',typeof module.exports);
console.log('exports==module.exports:   ',exports==module.exports);
console.log('exports===module.exports:  ',exports===module.exports);

l through comparison, it can be seen that it is an Object data, which indicates that it is a reference data, and that * * and = * * are equal, which indicates that the pointer address of the data store is the same.

Since the object that specifies exports is pointed to by default, whether the following operations can be performed.

//  Test JS file is written as follows
console.log('test Automatically executed when referenced')
var name='wolf';
//If you change the two to module Exports results are the same. In order to reduce the length of the article, only one is demonstrated
exports.name= name;
exports.drive=function(){
    console.log('Drive a car');
}
exports={
    age:36
}

//test_main.js
const test= require('./test.js');
console.log(test);

See the result:

It seems that an object named exports is assigned directly. It seems that the imported object is not overwritten, but it is changed as follows:

Then try the following:

//  Test JS file is written as follows
console.log('test Automatically executed when referenced')
var name='wolf';
//If you change the two to module Exports results are the same. In order to reduce the length of the article, only one is demonstrated
exports.name= name;
exports.drive=function(){
    console.log('Drive a car');
}
module.exports={
    age:36
}

//test_main.js
const test= require('./test.js');
console.log(test);

The returned results were found to be overwritten. Here you can see an example of exports and module Exports doesn't seem to point to one thing. How to understand this?

First of all, remember the following sentence: when nodejs refers to a module, the referenced object is module Exports

But why can exports also be used? Here we will talk about two types of data: basic data type and reference data type. If you don't know much about them, you can take a look at the previous article. Although it is about implicit conversion, it involves the explanation of two data types: Transmission array.

Exports is for the convenience of using the abbreviation, but essentially it is equivalent to writing a similar one: exports=module Exports This is quite a shallow copy. If you don't understand the deep copy and shallow copy, you can see another article: Transmission array.

The data of the example can be understood as follows:

Here are two things to remember:

  • The object referenced by require is the imported module Exports object
  • Module Since exports is an object, exports points to module Exports default address

Note: if you feel a little confused, just look at the two addresses on the top of me, and then look at this.

Let's take an example to understand it in detail (I suggest not to look at the results first):

//  Test JS file is written as follows
console.log('test Automatically executed when referenced')
var name='wolf';
//If you change the two to module Exports results are the same. In order to reduce the length of the article, only one is demonstrated
exports.name= name;
exports.drive=function(){
    console.log('Drive a car');
}
module.exports={
    age:36
}
exports=module.exports;
exports.address='No. 250, a street, a city';


//test_main.js
const test= require('./test.js');
console.log(test);

This article simply talks about some nodejs reference custom modules, as well as the noted exports and modules Exports. Later, we will talk about the use of several built-in modules, and then talk about the use of third-party modules and what npm is.

Tags: node.js Module

Posted by jonathen on Tue, 31 May 2022 07:46:31 +0530