--experimental-require-module not being respected in node22.x lambda runtime

0

Are there additional steps required to enable the --experimental-require-module functionality in the node22.x lambda runtime?

According to the node22.x runtime release announcement setting the --experimental-require-module flag in the NODE_OPTIONS environment variable should allow loading ESM modules with require from CommonJS modules.

The Node 22.12 LTS announcement suggests that this is actually now the default behavior and the --experimental-require-module flag should no longer be required.

However, when I deploy code relying on this behavior I receive a ERR_REQUIRE_ESM error and checking the process.features.require_module property within my lambda returns false indicating this feature is not enabled.

Checking the process.release property indicates that node 22.14.0 is in use which should support this flag.

asked a month ago126 views
1 Answer
-1

The Node.js 22 runtime provided by AWS Lambda does not have the experimental feature to detect module syntax automatically enabled. This feature, which allows loading ECMAScript modules (ESM) using require(), is not available in the Lambda-provided Node.js 22 runtime and cannot be enabled via an execution-time flag like --experimental-require-module.

The reason for this is that AWS Lambda's Node.js 22 runtime is intended for production workloads, and experimental features are not enabled in the Lambda build. This means that even if you set the NODE_OPTIONS environment variable with the --experimental-require-module flag, it will not have the desired effect.

To use this experimental feature in AWS Lambda, you would need to deploy your own custom Node.js runtime. This can be done in two ways:

  1. Using a custom runtime
  2. Deploying your function as a container image

In both cases, you would need to build your own version of Node.js with the experimental module syntax detection enabled.

It's important to note that while the Node.js 22.12 LTS announcement suggests this behavior is now default, AWS Lambda's runtime is specifically built without this experimental feature enabled for stability reasons.

If you need to use this feature, consider creating a custom runtime or container image where you have more control over the Node.js environment and can enable experimental features. However, be aware that using experimental features in a production environment may introduce instability or unexpected behavior.
Sources
Node.js 22 runtime now available in AWS Lambda | AWS Compute Blog

profile picture
answered a month ago
  • This is incorrect. The LLM is confusing two different parts of the blog I initially linked. One saying that the --experimental-require-module flag should work, and a later section saying that there is another experimental feature that is not supported.

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions