Skip to content

❗ NOTICE (aws-lambda-nodejs): Runtime.ImportModuleError: Cannot find module 'module' #30717

@ykageyama-mondo

Description

@ykageyama-mondo

Please add your +1 👍 to let us know you have encountered this

Status: RESOLVED

Fixed in v2.147.3.

Overview:

With the v0.22 release of esbuild, the default bundling behavior has been changed to omit imported packages from the bundle by default when the platform is node. The recommendation is to set the packages option to bundle to get the desired bundling behavior.

Complete Error Message:

Cannot find module 'source-map-support/register'\nRequire stack:\n- /var/task/index.js\n- /var/runtime/index.mjs

Workaround:

We recommend pinning the version to 0.2.15

nodejs_bundling_options = NodejsBundlingOptions(esbuild_version="0.21.5")

some_lambda = NodejsFunction(
    scope=self,
    ...
    bundling=nodejs_bundling_options,
)

Note: You may also add the flag --packages=bundle but this may result in other errors and is not recommended.

Solution:

We have pinned the version of esbuild used when no version is already installed to 0.2.15.


Original Issue Content:

Expected Behavior

When executing the lambda, import package from 'somePackage' imports the target package

Current Behavior

When executing the lambda, import package from 'somePackage' throws a Runtime.ImportModuleError with message Cannot find module 'somePackage'

Reproduction Steps

  1. Clone https://github.com/ykageyama-mondo/esbuild-issue-recreation
  2. Install and deploy
  3. Execute deployed lambda

Possible Solution

Add --packages=bundle to the default esbuildCommand created. Tried this on the reproduction lambda but seems to error with Must use "outdir" when there are multiple input files. I'm not familiar enough with esbuild to understand why this is happening and how to fix it.

Additional Information/Context

No response

CDK CLI Version

2.147.2

Framework Version

2.147.2

Node.js Version

18.17.1

OS

Ubuntu 22.04.4 LTS

Language

TypeScript

Language Version

5.5.2

Other information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    @aws-cdk/aws-lambda-nodejsbugThis issue is a bug.effort/mediumMedium work item – several days of effortin-progressThis issue is being actively worked on.management/trackingIssues that track a subject or multiple issuesp0

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions