CommonJS 是一种在服务器端运行的 JavaScript 模块化规范。它允许开发者将 JavaScript 代码分割成模块,以便更好地组织和管理代码。

模块导出

在 CommonJS 中,模块可以通过 module.exports 对象导出值。

// myModule.js
function sayHello() {
  console.log('Hello!');
}

module.exports = {
  sayHello: sayHello
};

模块导入

要导入一个模块,可以使用 require 函数。

// main.js
const myModule = require('./myModule');

myModule.sayHello();

图片示例

下面是一个示例图片,展示了 CommonJS 模块的概念。

CommonJS

扩展阅读

想要了解更多关于 CommonJS 的内容,可以访问CommonJS 官方文档

# CommonJS Module Programming

CommonJS is a JavaScript modularization specification for server-side execution. It allows developers to split JavaScript code into modules for better organization and management.

## Module Export

In CommonJS, modules can export values through the `module.exports` object.

```javascript
// myModule.js
function sayHello() {
  console.log('Hello!');
}

module.exports = {
  sayHello: sayHello
};

Module Import

To import a module, use the require function.

// main.js
const myModule = require('./myModule');

myModule.sayHello();

Image Example

Below is an example image illustrating the concept of CommonJS modules.

CommonJS

Further Reading

For more information about CommonJS, you can visit the official CommonJS documentation.