【发布时间】:2021-06-18 12:16:46
【问题描述】:
我在 Lambda 上遇到了一个非常不幸的错误:
Unable to import module 'lib/index': Error
at require (internal/module.js:20:19)
这很奇怪,因为肯定有一个名为 handler 的函数从 lib/index 导出...不确定整个子目录是否对其他人来说是个问题,所以我想问一下。
sam-template.yaml
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: Does something crazy
Resources:
SomeFunction:
Type: AWS::Serverless::Function
Properties:
Handler: lib/index.handler
Role: arn:aws:iam::...:role/lambda-role
Runtime: nodejs6.10
Timeout: 10
Events:
Timer:
Type: Schedule
Properties:
Schedule: rate(1 minute)
模块结构
|-- lib
| `-- index.js
`-- src
`-- index.js
我将它嵌套在这里是因为我在构建过程中使用以下代码转换 ES6,摘自package.json:
"build": "babel src -d lib"
buildspec.yaml
version: 0.1
phases:
install:
commands:
- npm install
- aws cloudformation package --template-file sam-template.yaml --s3-bucket some-bucket --output-template-file compiled-sam-template.yaml
build:
commands:
- npm run build
post_build:
commands:
- npm prune --production
artifacts:
files:
- 'node_modules/**/*'
- 'lib/**/*'
- 'compiled-template.yaml'
【问题讨论】:
标签: node.js amazon-web-services aws-lambda