Skip to content

Bug: How to fix "Dynamic require of "*" is not supported" when using esbuild to deploy Typescript in esm format #4827

@peejayess

Description

@peejayess

Description:

I am deploying a Typescript lambda in esm format using the SAM esbuild feature and at runtime I am getting the "Dynamic require of "*" is not supported" where * are the node built in packages. In my case, this is being caused by the @opensearch-project package but it could be caused by any cjs package using the node inbuilt packages.

This appears to be a well known problem with esbuild, with many suggested workarounds but no apparent fix. One of the many suggested fixes is to add a banner: import { createRequire } from 'module';const require = createRequire(import.meta.url); but banner is not one of the SAM esbuild build properties.

Are there any suggested work arounds for this?

Steps to reproduce:

SAM esbuild meta:

Metadata:
      BuildMethod: esbuild
      BuildProperties:
        Format: esm 
        Minify: false
        OutExtension:
          - .js=.mjs
        Target: "es2022"
        Sourcemap: true        
        EntryPoints: 
          - src/search_objects/search_objects.ts

My search_objects typescript handler has an import:
import { Client } from "@opensearch-project/opensearch";

Observed result:

ERROR	Uncaught Exception 	
{
    "errorType": "Error",
    "errorMessage": "Dynamic require of \"events\" is not supported",
    "stack": [
        "Error: Dynamic require of \"events\" is not supported",
        "    at file:///var/task/search_objects.mjs:13:9",
        "    at node_modules/@opensearch-project/opensearch/index.js (/private/var/folders/qv/s3dr8wns2wb13nhymcbbpp580000gn/T/tmpdohwfrfl/node_modules/@opensearch-project/opensearch/index.js:33:26)",
        "    at __require2 (file:///var/task/search_objects.mjs:19:51)",
        "    at <anonymous> (/private/var/folders/qv/s3dr8wns2wb13nhymcbbpp580000gn/T/tmpdohwfrfl/node_modules/@opensearch-project/opensearch/index.mjs:31:17)",
        "    at ModuleJob.run (node:internal/modules/esm/module_job:194:25)"
    ]
}

Expected result:

The imports of node inbuilt packages to work correctly

Additional environment details (Ex: Windows, Mac, Amazon Linux etc)

sam --info:

{
  "version": "1.76.0",
  "system": {
    "python": "3.8.13",
    "os": "macOS-13.2.1-arm64-arm-64bit"
  },
  "additional_dependencies": {
    "docker_engine": "Not available",
    "aws_cdk": "Not available",
    "terraform": "Not available"
  }
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions