Is it possible to utilize modules (such as sharp) that are exported as export = someModule
in a Lambda function defined with the NodejsFunction
from the aws-cdk-lib
?
It seems like the require statement (const xxx = require('module')
) does not work with the TypeScript code for Lambdas that is bundled by CDK.
Both of the attempted import methods resulted in an error.
import sharp from 'sharp'
import * as sharp from 'sharp'
import sharp = require('sharp')
An error occurred while installing the "sharp" module
Unable to locate module '../build/Release/sharp-linux-x64.node'
Require stack:
- /var/task/index.js
- /var/runtime/index.mjs
The Lambda function is defined in the CDK code as shown below.
import { aws_lambda_nodejs as lambda } from 'aws-cdk-lib'
const fn = new lambda.NodejsFunction(scope, 'fn-id', {
entry: 'lib/lambda/my-fn.ts',
functionName: 'fn-name'
})