You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用AWS CDK NodejsFunction时部分依赖模块运行失败的问题

问题:AWS CDK NodejsFunction创建Lambda后sam local invoke部分模块报错"File not found"

我通过AWS CDK的NodejsFunction创建了一个Lambda函数,在CDK的node_modules目录中安装了Lambda需使用的依赖模块。执行sam local invoke命令时,部分模块可正常运行,部分模块运行失败,报错信息显示“File not found in '/var/task/...'”。请问是否部分模块无法与NodejsFunction兼容?

相关代码示例:

lambda-stack.ts

new lambda.NodejsFunction(this, 'SampleFunction', {
  runtime: Runtime.NODEJS_14_X,
  entry: 'lambda/sample-function/index.ts'
})

使用date-fns模块(运行成功)

import { format } from 'date-fns'

export const handler = async () => {
  try {
    console.log(format(new Date(), "'Today is a' eeee"))
  } catch (error) {
    console.log(error)
  }
}

使用chrome-aws-lambda模块(运行失败)

const chromium = require('chrome-aws-lambda')

export const handler = async () => {
  try {
    const browser = await chromium.puppeteer.launch()
  } catch (error) {
    // Cannot find module '/var/task/puppeteer/lib/Browser'
    console.log(error)
  }
}

使用pdfkit模块(运行失败)

const PDFDocument = require('pdfkit')

export const handler = async () => {
  try {
    const doc = new PDFDocument()
  } catch (error) {
    // no such file or directory, open '/var/task/data/Helvetica.afm'
    console.log(error)
  }
}

内容的提问来源于stack exchange,提问作者kusumoto_teruya

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.26 00:43:26