serverless: V4: dev mode throws error when function invoked

Issue description

After running serverless dev on a simple service, running serverless invoke on a function in the service or invoking it by making an HTTP request causes dev mode to fail with an error. Full serverless support output in Context below.

Error received:

Error: Cannot find module '/opt/sls-sdk-node'
Require stack:
- internal/preload
    at Module._resolveFilename (node:internal/modules/cjs/loader:1143:15)
    at Module._load (node:internal/modules/cjs/loader:984:27)
    at internalRequire (node:internal/modules/cjs/loader:174:19)
    at Module._preloadModules (node:internal/modules/cjs/loader:1574:5)
    at loadPreloadModules (node:internal/process/pre_execution:723:5)
    at setupUserModules (node:internal/process/pre_execution:203:5)
    at prepareExecution (node:internal/process/pre_execution:159:5)
    at prepareMainThreadExecution (node:internal/process/pre_execution:54:10)
    at node:internal/main/run_main_module:11:19 {
  code: 'MODULE_NOT_FOUND',
  requireStack: [ 'internal/preload' ]
}

Node.js v20.12.2

✖ ENOENT: no such file or directory, open '/tmp/sls_3329370.json'
Error: ENOENT: no such file or directory, open '/tmp/sls_3329370.json'

Context

Service Overview

  • Serverless Framework Version: 4.0.26
  • Service Config File: serverless.yml
  • Service Name: test-http-api2
  • Service App: test-http-api
  • Service Runtime: nodejs20.x
  • Service Stage: dev
  • Service Region: us-east-1
  • Command: invoke --function hello2

Service Path

/home/garethm/Projects/testing/test-http-api

Service Config

org: xxx
app: test-http-api
service: test-http-api2
provider:
  timeout: 20
  name: aws
  stage: dev
  runtime: nodejs20.x
  deploymentBucket: 
    name: xxxxxxxx
functions:
  hello2:
    handler: handler.hello
    events:
      - http:
          method: get
          path: /

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 1
  • Comments: 20 (9 by maintainers)

Most upvoted comments

Thank you @austencollins - that works as advertised.

Dev mode is super useful!

The actual stack trace is a little different. Maybe this will help:

─ node:internal/modules/cjs/loader:1147
  throw err;
  ^

Error: Cannot find module '/opt/sls-sdk-node'
Require stack:
- internal/preload
    at Module._resolveFilename (node:internal/modules/cjs/loader:1144:15)
    at Module._load (node:internal/modules/cjs/loader:985:27)
    at internalRequire (node:internal/modules/cjs/loader:175:19)
    at Module._preloadModules (node:internal/modules/cjs/loader:1582:5)
    at loadPreloadModules (node:internal/process/pre_execution:731:5)
    at setupUserModules (node:internal/process/pre_execution:172:3)
    at prepareExecution (node:internal/process/pre_execution:132:5)
    at prepareMainThreadExecution (node:internal/process/pre_execution:55:10)
    at node:internal/main/run_main_module:11:19 {
  code: 'MODULE_NOT_FOUND',
  requireStack: [ 'internal/preload' ]
}

Node.js v20.10.0
← λ api

Hello. I am getting the same error too. its pretty frustating. (Also using ts) Serverless ϟ Support

This generates a report from your last Serverless Framework command (including any errors) to use for Github Issues, debugging w/ AI, or creating a support ticket w/ Serverless Inc.

Summary Report -----------------

Service Overview

  • Serverless Framework Version: 4.1.22
  • Command:

Service Overview

  • Serverless Framework Version: 4.1.21
  • Service Config File: lambda.yml
  • Service Name: mainThreads
  • Service App: conversaly-api
  • Service Runtime: nodejs20.x
  • Service Stage: dev
  • Service Region: us-east-1
  • Command: deploy --config mainThreads/lambda.yml

Service Config

# "org" ensures this Service is used with the correct Serverless Framework Access Key.
org: neuralcreative
# "app" enables Serverless Framework Dashboard features and sharing them with other Services.
app: conversaly-api
# "service" is the name of this project. This will also be added to your AWS resource names.
service: mainThreads
provider:
  name: aws
  runtime: nodejs20.x
functions:
  getAssistantReply:
    handler: handler.getAssistantReply

Service Overview

  • Serverless Framework Version: 4.2.4
  • Service Config File: serverless.yml
  • Service Name: conversaly-mainThreads
  • Service App: conversaly
  • Service Runtime: nodejs20.x
  • Service Stage: dev
  • Service Region: us-east-1

Command

dev --config src/mainThreads/serverless.yml,–stage dev

Service Config

# "org" ensures this Service is used with the correct Serverless Framework Access Key.
org: neuralcreative2
# "app" enables Serverless Framework Dashboard features and sharing them with other Services.
app: conversaly
# "service" is the name of this project. This will also be added to your AWS resource names.
service: conversaly-mainThreads
provider:
  name: aws
  region: us-east-1
  runtime: nodejs20.x
  timeout: 25
  stage: ${opt:stage, 'dev'}
  stackName: ${self:service}-${self:provider.stage}-stack


stages:
  prod:
    observability: true
  dev:
    observability: false
  default:
    # Disabales observability in all other stages
    observability: true

functions:
  processTools: 
    name: ${self:service}-${self:provider.stage}-processTools
    handler: processTools/handler.processTools
    events:
      - httpApi:
          method: post
          path: /processTools
          authorizer:
            type: aws_iam
    environment:
      
  getAssistantReply:
    name: ${self:service}-${self:provider.stage}-getAssistantReply
    handler: handler.getAssistantReply
    events:
      - httpApi:
          method: post
          path: /getAssistantReply
          authorizer:
            type: aws_iam
    environment:
      DEVSESSION: "false"
      RUN_STAGE: ${sls:stage}

Same here

i am getting the exact same error