Skip to main content

How to Use ES Module

Introduction

In the Tencent Cloud Functions Node.js environment, code cannot be directly written using the ES Module specification. The primary reason is that the entry file (index.js) supported by cloud functions by default must follow the CommonJS specification, and the file name must be index.js. However, Node.js requires modules conforming to the ES Module specification to have the .mjs file extension. Therefore, 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 through module.exports. For ES Module modules using import and export, Node.js cannot directly recognize them. 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 in that directory will be interpreted as ES Module modules.

This document describes how to write code compliant with the ES Module specification in the cloud functions Node.js environment, and how to refactor existing cloud function code to adapt to the ES Module specification. It covers the following two usage scenarios:

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

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

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

How to Write Code Compliant with the ES Module Specification in New Cloud Functions

Creating a Nodejs v14 Environment Cloud Function

Note

Nodejs version must be >=14.

Creating an 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. It is imported using ES Module syntax in entry.mjs and called within the entry function entry.

Importing the 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 ES Module Specification

For existing cloud function code that needs to adapt to the ES Module specification, following the principle of minimal invasiveness to the code and lowest refactoring cost, we can proceed as follows:

Creating an 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 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. We import the business code using ES Module syntax in entry.mjs and call it in the entry function entry.

Creating a package.json file

Since Node.js requires ES6 modules to use the .mjs file extension, manually changing all existing code file extensions to .mjs would be too cumbersome. We can create a file named package.json in the business code directory with the following content:

{
"type": "module"
}

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

The complete code directory structure is as follows:

Summary

This document details how to write and refactor code in the cloud functions Node.js environment to adapt to the ES Module specification, covering two main scenarios:

  • Writing a new cloud function:

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

    • Dynamically importing and calling entry.mjs in index.js.

  • Refactoring existing cloud functions:

    • Similarly creating entry.mjs as the entry point.

    • Add package.json specifying "type": "module" to avoid changing all file extensions.