Skip to content

lambda-nodejs: Bundling fails with recent pnpm version #25612

@cchanche

Description

@cchanche

Describe the bug

When bundling a lambda function using lambda_nodejs with no dependencies and with pnpm version 8.4.0 or higher, the script fails to delete the node_modules/.modules.yaml file.

Expected Behavior

The bundling succeeds (no need to delete this file).

Current Behavior

The bundling fails with error :

Bundling asset stack/lambda-fn/Code/Stage...

  ...dk.out/bundling-temp-d1f50916a16a3a6ba493bb8442e9fc9680e83df6b64d7a5183903277f0923fc7/index.js  2.1kb

⚡ Done in 8ms
Already up to date
Done in 314ms
rm: [..]/cdk.out/bundling-temp-d1f50916a16a3a6ba493bb8442e9fc9680e83df6b64d7a5183903277f0923fc7/node_modules/.modules.yaml: No such file or directory

...

Error: Failed to bundle asset stack/lambda-fn/Code/Stage, bundle output is located at [...] exited with status 1

Reproduction Steps

Simple lambda construct :

new lambda.NodejsFunction(this, 'MyFunction', {
  runtime: lambda.Runtime.NODEJS_16_X,
  entry: path.join(__dirname, 'lambda', 'src/index.ts'),
});

/lambda/package.json

{
  "dependencies": {
    "aws-lambda": "^1.0.7"
  },
}

/lambda/src/index.ts

import { DefineAuthChallengeTriggerHandler } from 'aws-lambda';

export const handler: DefineAuthChallengeTriggerHandler = async (event) => {
  return event;
};

/package.json

{
  "dependencies": {
    "aws-cdk": "^2.70.0",
    "aws-cdk-lib": "2.67.0",
    "constructs": "^10.0.0",
  },
  "devDependencies": {
    "esbuild": "^0.17.11",
    "ts-node": "^10.0.0",
    "typescript": "4.9.5"
  }
}

pnpm version :

  • v8.4.0 -> bundling fails
  • below v8.4.0 -> bundling succeeds

Possible Solution

In the bundling.ts source file :

  • add -f flag to the rm command
  • don't perform the rm command if pnpm -v >= 8.4.0

Additional Information/Context

The pnpm v8.0.4 release note specifies clearly :

Do not create a node_modules folder with a .modules.yaml file if there are no dependencies inside node_modules.

Since esbuild bundles the dependencies (unless specified otherwise through lambda_nodejs.NodejsFunctionProps['bundling]['nodeModules']), this will break synthesis of most lambda functions.

CDK CLI Version

2.70.0 (build c13a0f1)

Framework Version

No response

Node.js Version

v16.19.1

OS

macOs 13.3.1 (22E261)

Language

Typescript

Language Version

Typescript (4.9.5)

Other information

Source file is located at packages/aws-cdk-lib/aws-lambda-nodejs/lib/bundling.ts

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