无服务器捆绑包缺少依赖项

serverless-bundle Missing Dependencies

我无法使用无服务器捆绑插件生成捆绑包。

tsconfig.json

{
 "compilerOptions": {
    "lib": ["es2017"],
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,

    "target": "es2017",
    "outDir": "dist",
    "declaration": true,
    "sourceMap": true,
    "baseUrl": ".",

    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "allowJs": false,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true
  },
  "exclude": ["./dist/**/*", "./node_modules/**/*"]
}

serverless.yml

service: serverless-boilerplate

custom:
  # Our stage is based on what is passed in when running serverless
  # commands. Or falls back to what we have set in the provider section.
  stage: ${opt:stage, 'offline'}
  region: ${opt:region, 'us-east-1'}
  bundle:
    linting: false
  serverless-offline:
    httpPort: 4000
    noPrependStageInUrl: true

provider:
  name: aws
  runtime: nodejs14.x
  memorySize: 512
  timeout: 10
  logRetentionInDays: 90
  lambdaHashingVersion: 20201221 # https://www.serverless.com/framework/docs/deprecations/#LAMBDA_HASHING_VERSION_V2
  logs:
    httpApi: true
  httpApi:
    # metrics: true # Enable if you need
    cors: true

functions:
  app:
    handler: src/handler.handler
    # reservedConcurrency: 100
    events:
      - httpApi:
          path: '/{proxy+}'
          method: '*'

package:
  individually: true

plugins:
  - serverless-dotenv-plugin
  - serverless-bundle
  - serverless-offline

这是运行sls package

之后的.serverless/app.zip文件

如您所见,没有 node_modules 文件夹。

如果我使用 sls offline start --stage offline

启动应用程序

它看起来像依赖项之一试图读取 node_modules 中它自己的包文件夹中的目录(资源):

但是由于 serverless-bundle 将所有内容合并 handler.js 中,没有 node_modules 文件夹和依赖文件夹,所以 readdir() 失败了.

我做错了什么?

似乎在 serverless.yml 中将 externals: all 添加到 custom.bundle 解决了问题。