Search code examples
serverlessserverless-framework

serverless-bundle Missing Dependencies


I am failing to generate a bundle with serverless-bundle plugin.

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

Here is the .serverless/app.zip file after running sls package

enter image description here

As you see there is no node_modules folder.

If I launch the app with sls offline start --stage offline

enter image description here

It looks like one of the dependencies trying to read a directory (resources) in its own package folder in node_modules: enter image description here

But since serverless-bundle merges everything into handler.js , there are no node_modules folder and dependency folders, so readdir() is failing.

What I am doing wrong?


Solution

  • Looks like adding externals: all to custom.bundle in serverless.yml fixed the issue.