AWS CDK - Cannot find module 'sharp' error on AWS lambda

When we add the dependency sharp with the Lambda function we are getting the following error in the Cloudwatch. I faced this issue while deploying the Lambda functions through AWS CDK.

2022-09-02T05:14:16.576Z	undefined	ERROR	Uncaught Exception 	{
    "errorType": "Error",
    "errorMessage": "\nSomething went wrong installing the \"sharp\" module\n\nCannot find module '../build/Release/sharp-linux-x64.node'\nRequire stack:\n- /var/task/index.js\n- /var/runtime/index.mjs\n\nPossible solutions:\n- Install with verbose logging and look for errors: \"npm install --ignore-scripts=false --foreground-scripts --verbose sharp\"\n- Install for the current linux-x64 runtime: \"npm install --platform=linux --arch=x64 sharp\"\n- Consult the installation documentation: https://sharp.pixelplumbing.com/install",
    "stack": [
        "Error: ",
        "Something went wrong installing the \"sharp\" module",
        "",
        "Cannot find module '../build/Release/sharp-linux-x64.node'",
        "Require stack:",
        "- /var/task/index.js",
        "- /var/runtime/index.mjs",
        "",
        "Possible solutions:",
        "- Install with verbose logging and look for errors: \"npm install --ignore-scripts=false --foreground-scripts --verbose sharp\"",
        "- Install for the current linux-x64 runtime: \"npm install --platform=linux --arch=x64 sharp\"",
        "- Consult the installation documentation: https://sharp.pixelplumbing.com/install",
        "    at asset-input/node_modules/sharp/lib/sharp.js (/var/task/index.js:37183:13)",
        "    at __require (/var/task/index.js:9:50)",
        "    at asset-input/node_modules/sharp/lib/constructor.js (/var/task/index.js:37196:5)",
        "    at __require (/var/task/index.js:9:50)",
        "    at asset-input/node_modules/sharp/lib/index.js (/var/task/index.js:40895:17)",
        "    at __require (/var/task/index.js:9:50)",
        "    at Object.<anonymous> (/var/task/index.js:40971:28)",
        "    at Module._compile (node:internal/modules/cjs/loader:1105:14)",
        "    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1159:10)",
        "    at Module.load (node:internal/modules/cjs/loader:981:32)"
    ]
}

The cause of this problem is the node_modules directory of the deployment package must include binaries for the Linux x64 platform. We can solve this problem by adding bundling option to the NodejsFunction. Please find the code below. forceDockerBundling will bundle the sharp package for the Lambda runtime environment.

// Resize image Lambda function creation & configuration
const imageResizerLamba = new NodejsFunction(this, "ImageResizerLambda", {
  runtime: lambda.Runtime.NODEJS_16_X,
  handler: "generateThumbs",
  functionName: "ImageResizerLambda",
  reservedConcurrentExecutions: 1,
  entry: path.resolve(`./src/handlers/image-resizer.ts`),
  timeout: cdk.Duration.seconds(900),
  environment: {
    IMG_BUCKET_NAME: imageBucket.bucketName,
    THUM_IMG_BUCKET_NAME: thumImageBucket.bucketName,
  },
  bundling: {
    nodeModules: ["sharp"],
    forceDockerBundling: true,
  },
});

References: https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.BundlingOptions.html/ https://sharp.pixelplumbing.com/install/