Skip to main content

How to Use ES Module

Preface

In the Node.js environment of cloud functions, it is not possible to directly use the ES Module specification to write code. The main reason is that the default entry file supported by cloud functions (index.js) must follow the CommonJS specification, and the file name must be 'index.js'. However, Node.js requires that module files conforming to the ES Module specification have the extension .mjs. This means that in the entry file of cloud functions, we cannot directly use the ES Module specification for coding. Additionally, Node.js natively supports CommonJS modules, which are loaded via require and exported via module.exports. For ES Module modules using import and export, Node.js cannot recognize them directly. To enable Node.js to correctly identify these modules, their file extensions must be changed to .mjs, allowing Node.js to recognize them as ES Module modules. If you prefer not to change the file extension to .mjs, you can specify the type field as module in the project's package.json file. Once set, the JS scripts within that directory will be interpreted as ES Module modules.

This document introduces how to write code that conforms to the ES Module specification in the cloud function Node.js environment, and how to refactor existing cloud function code to adapt to the ES Module specification. This article will start from the following two usage scenarios:

  • Scenario 1: How to write code that conforms to the ES Module specification in a new cloud function.

  • Scenario 2: Refactor existing cloud function code to support the ES Module specification.

Through a detailed analysis of the above two scenarios, you will be able to fully grasp the methods and techniques of applying the ES Module specification in the cloud function Node.js environment.

How to Write ES Module-Compliant Code in a New Cloud Function

Create a Node.js v14 environment cloud function

Tip

Node.js version must be greater than or equal to 14.

Create Entry File entry.mjs

Create a file named entry.mjs as the ES Module entry point. This file will use ES Module syntax to import other modules and export a function.

// entry.mjs
import { foo } from './util.js';

// Entry function
export const entry = (event, context) => {
foo(event, context);
};

In the above code, assume that the foo function in the util.js module is our business logic code. In entry.mjs, we import the foo function using ES Module syntax and call it in the entry function entry.

Import Entry File

Next, in index.js, you need to dynamically import the entry.mjs module and call its exported entry function.

// index.js
exports.main = async (event, context) => {
const { entry } = await import('./entry.mjs');
return entry(event, context);
};

The complete code directory structure is as follows:

Refactoring Existing Cloud Function Code to Support the ES Module Specification

For existing cloud function code to be adapted to the ES Module specification, following the principle of minimal code intrusion and lowest refactoring cost, we can proceed as follows:

Create Entry File entry.mjs

Create a file named entry.mjs as the ES Module entry point. This file will use ES Module syntax to import existing business code.

// entry.mjs

// Assume the src folder contains the existing business logic code.
import { foo } from './src';

// Entry function
export const entry = (event, context) => {
foo(event, context);
};

In the above code, assume that the src folder contains the business logic code. In entry.mjs, we import the business code using ES Module syntax and call it in the entry function entry.

Create package.json File

Since Node.js requires that ES6 modules must use the .mjs file extension, it would be too cumbersome to manually change all existing code file extensions to .mjs one by one. Instead, we can create a file named package.json in the business code directory and write the following content:

{
"type": "module"
}

This will tell Node.js to treat this directory as an ES Module, allowing you to import and export modules using ESM syntax.

The complete code directory structure is as follows:

Summary

This document details how to write and refactor code in the Node.js environment of cloud functions to adapt to the ES Module specification. It primarily covers two scenarios:

  • Writing new cloud functions:

    • Create entry.mjs as the ES Module entry point.

    • Dynamically import and call entry.mjs in index.js.

  • Refactoring existing cloud functions:

    • Similarly, create entry.mjs as the entry point.

    • Add package.json to specify "type": "module", avoiding the need to change all file extensions.