Home  >  Article  >  Web Front-end  >  What is CommonJS? How to customize modules in Nodejs?

What is CommonJS? How to customize modules in Nodejs?

藏色散人
藏色散人forward
2022-08-08 14:22:182214browse

1. What is CommonJs?

JavaScript is a powerful object-oriented language with many fast and efficient interpreters. However, the JavaScript standard defines APIs for building browser-based applications. There is no standard library developed for a wider range of applications. The CommonJS specification was proposed mainly to make up for the shortcomings of the current JavaScript lack of standards. Its ultimate goal is to provide a standard library for languages ​​​​similar to Python, Ruby and Java, rather than just staying at the stage of small script programs. Applications written with the CommonJS API can not only use JavaScript to develop client applications, but also write the following applications.

  • Server-side JavaScript applications. (nodejs)
  • Command line tool.
  • Desktop graphical interface application.

CommonJS is the standard for modularization, and nodejs is the implementation of CommonJS (modularization).

2. Modularization in Nodejs

Node applications are composed of modules and adopt the CommonJS module specification.

2.1 In Node, modules are divided into two categories:

One type is the module provided by Node, called the core module; the other type is the module written by the user, called the file module.

  • The core module part is compiled into a binary executable file during the compilation process of Node source code. When the Node process starts, some core modules are loaded directly into the memory. Therefore, when this part of the core module is introduced, the two steps of file location and compilation and execution can be omitted, and priority is determined in the path analysis, so its loading The speed is the fastest. For example: HTTP module, URL module, and Fs module are all built-in core modules of nodejs and can be directly introduced and used.
  • The file module is dynamically loaded at runtime, requiring complete path analysis, file location, compilation and execution process. The speed is slightly slower than the core module, but it is used a lot. These modules need to be defined by ourselves. Next we take a look at custom modules in nodejs.

2.2 The provisions of custom modules in CommonJS (Nodejs):

  • 1. We can extract the public functions into a separate js file as a module, By default, the methods or properties in this module are not accessible from the outside. If you want the outside world to access the methods or properties in the module, you must expose the properties or methods in the module through exports or module.exports.
  • 2. In the files that need to use these modules, introduce this module through require. At this time, you can use the properties and methods exposed in the module.

2.3 Define the use module:

// 定义一个 tools.js 的模块
//模块定义
var tools = {
sayHello: function() {
return 'hello NodeJS';
},
add: function(x, y) {
return x + y;
}
};
// 模块接口的暴露
// module.exports = tools;
exports.sayHello = tools.sayHello;
exports.add = tools.add;
1
2
3
4
var http = require('http');
// 引入自定义的 tools.js 模块
var tools= require('./tools');
tools.sayHello(); //使用模块

3. npm init generates package.json

npm init --yes

[Recommended: node.js video tutorial

The above is the detailed content of What is CommonJS? How to customize modules in Nodejs?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:jb51.net. If there is any infringement, please contact admin@php.cn delete