aws-cdk: :exclamation: NOTICE (aws-lambda-nodejs): Runtime.ImportModuleError: `Cannot find module 'module'`
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
- Clone https://github.com/ykageyama-mondo/esbuild-issue-recreation
- Install and deploy
- 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
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 59
- Comments: 23 (9 by maintainers)
Commits related to this issue
- fix(lambda-nodejs): breaking change in esbuild causes import module error (#30726) ### Issue # (if applicable) Closes #30717. ### Reason for this change esbuild introduced a breaking change in v0.... — committed to aws/aws-cdk by xazhao 2 years ago
- fix(lambda-nodejs): breaking change in esbuild causes import module error (#30726) ### Issue # (if applicable) Closes #30717. ### Reason for this change esbuild introduced a breaking change in v0.... — committed to aws/aws-cdk by xazhao 2 years ago
- chore: notice for https://github.com/aws/aws-cdk/issues/30717 (#550) Update notice for https://github.com/aws/aws-cdk/issues/30717 — committed to cdklabs/aws-cdk-notices by xazhao 2 years ago
Esbuild released a new version that reverts the change (0.23), so that it now behaves the same as pre-0.22.
Despite all this, I think CDK should consider pinning to a known working version at least in the docker-based build.
Another temporary solution is to add the now required parameter instead of specifying the version. This will keep the old bundling functionality working with the new esbuild version.
In JS/TS this would look somewhat like this:
As there are already some people liking my proposed solution, hereās something for your consideration: I personally prefer this approach, because it avoids locking esbuild to an outdated version, which might also be easy to forget and stay like that for longer than intended. However, I expect that a proper fix of the underlying issue by the aws-cdk lib might very well be in conflict with this setting. I guess it will either include packages in the lambda bundle or (hopefully) it will add the
--packages=bundleparameter by default, which might lead to errors in builds once we get the cdk update. This is all speculation of course, but if you value a high certainty of perfectly running pipelines over ensuring up to date dependencies, then you should probably go with @mrgrainās approach and hard-code the esbuild version. Just make sure you donāt forget to revert when this issue is resolved.One more thing you should know is that setting the esbuild version in cdk code only works when using docker for bundling. This is the default, so it should work for most people. However, if your cdk project is running in a node environment where esbuild is already installed and you havenāt specified the
forceDockerBundlingbundling option, it will run with the installed version, no matter what you specify. Of course, then itās still your decision whether you lock the esbuild version or add the bundling option.Edit: I just realized that the fact about using the esbuild version present in a node project can be very useful for large projects, as it provides the opportunity to set the esbuild version for the whole project in one place instead of setting it in every NodejsFunction constructor.
I would also like to share some of my expectations and opinions on this matter.
Absolutely agree. ESBuild doesnāt follow semantic versioning and uses minor versions for breaking changes, as stated by the maintainer in #3817:
In the release notes of 0.23 he also strongly advocates on pinning the version:
However, I would also like to point out that any build dependencies have to be pinned to expected versions, in order to avoid any unexpected system outages like we had this time. Thereās nothing worse, than having a version deployed to a dev/staging system that works and deploying the same version without changes a day later to production and having an outage š.
For example, I saw this line in the Docker build logs, which suggests that TypeScript is also not pinned to any version (not even a major version):
Maybe other dependencies are affected as well, so it would be great, if you could check this.
As long as the build/stack synth fails, Iām personally fine with this, as long as thereās an expressive error message telling me what to do. For me itās much more preferable to see a build time error and a broken pipeline, than an unexpected runtime error in a production system š.
Hey, aws guys. This is really a critical issue. I have a huge CDK project and I cannot add
--packages=bundleto every NodejsFunction resource. So please set it as the default bundling behavior in CDK or provide us a global option for setting it. I will lock esbuild to 0.21.5 for now but I want to know your decision.Another comment.
a) NodejsFunction is cool. b) AWS is a $2 trillion stock value company.
Therefore, why not just buy ESBUILD, or create your own, and roll it into CDK CLI?
Also, the more AWS controls the code, the more it can control the DevSecOps pipeline, the (hopefully) more reliable the CDK, and the minimisation of these 3rd party derived issues. Right?
I am not sure if this issue should be marked a closed. The fix to pin the esbuild version in the docker file does not resolve the issue where the local bundling is broken by default for esbuild version 0.22.
We are currently in the process of creating a patch release with the esbuild version pinned to
0.21.5to get customers out of immediate pain. We will also look at adding the--packages=bundleflag in but we will not include that in the patch release in case there are unintended consequences that also be customer impacting.Everyone looking for a temporary fix, I recommend doing this until we have settled on a solution in CDK:
We made the following change and that fixed the issue
Issue reported on
esbuildgh as wellhttps://github.com/evanw/esbuild/issues/3817