aws-lambda-nodejs: package.json type field is not preserved with external node_modules #32777
Labels
@aws-cdk/aws-lambda-nodejs
bug
This issue is a bug.
needs-triage
This issue or PR still needs to be triaged.
Describe the bug
Hello 👋
I am using https://knexjs.org/ to apply database migrations via Lambda. This requires some modules to be loaded externally due to dynamic import which is working well with CommonJS.
However when porting the code to ES Modules (and using
OutputFormat.ESM
) I am seeing some module loading issues due to thetype
key not being set in the generatedpackage.json
file. I traced this back to this line:aws-cdk/packages/aws-cdk-lib/aws-lambda-nodejs/lib/bundling.ts
Line 288 in ac3ffa5
This is surprising as it implicitly changes the output format of the bundle back to CommonJS.
Regression Issue
Last Known Working CDK Version
No response
Expected Behavior
The
type
property should be added to thepackage.json
file that is generated when using thenodeModules
option, but only when in the sourcepackage.json
file.Current Behavior
No
type
property is added to the generatedpackage.json
.Reproduction Steps
index.ts
can have any contents to replicate this issue.Possible Solution
The
type
property is also added to the generatedpackage.json
when using thenodeModules
option and only if it is present in the sourcepackage.json
. However it may be worth considering to be explicit here as per the Node.js recommendation (and setting thetype
based on the value ofOutputType
):Additional Information/Context
There is a workaround here which is to write the source code in
.mjs
or.mts
explicitly and not rely on thepackage.json
lookup to determine the default module system that.js
uses.CDK CLI Version
v2.174.0
Framework Version
v2.174.0
Node.js Version
v20.18.0
OS
Mac OS X
Language
TypeScript
Language Version
TypeScript 5.7.2
Other information
No response
The text was updated successfully, but these errors were encountered: