Skip to content

lambda-nodejs: Bundling fails pnpm version >= 8.4.0 #26478

@damienhill

Description

@damienhill

Describe the bug

Previous issue #25612.

When bundling a lambda function using lambda_nodejs with no dependencies and with pnpm version 8.4.0 or higher the bundling process fails as the rm command doesn't correctly handle missing file.

My recent pull request to fix this was merged into release 2.88.0 but unfortunately failed to resolve issue as the -f flag was appended after the path which still throws an error when file doesn't exist.

Expected Behavior

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

Current Behavior

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 the -f flag should come before the path in the rm command so the command doesn't error on file not existing.

aws-cdk/packages/aws-cdk-lib/aws-lambda-nodejs/lib/bundling.ts

isPnpm ? osCommand.remove(pathJoin(options.outputDir, 'node_modules', '.modules.yaml')) + ' -f' : '', // Remove '.modules.yaml' file which changes on each deployment

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.88.0 (build 5d497f9)

Framework Version

No response

Node.js Version

v16.19.1

OS

macOs 13.3.1 (22E261)

Language

Typescript

Language Version

TypeScript (5.0.4)

Other information

No response

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