dherault / serverless-offline

Emulate AWS λ and API Gateway locally when developing your Serverless project
MIT License
5.19k stars 796 forks source link

feat: add support for serverless v4 and typescript #1806

Closed DorianMazur closed 1 month ago

DorianMazur commented 1 month ago

Description

When using Serverless v4 with TypeScript, the following error occurs:

✖ Unhandled exception in handler 'hello'.
✖ Error: Cannot find module 'index'
Require stack:
- /Users/dorianmazur/Documents/simple-serverless4-offline-example/node_modules/serverless-offline/src/lambda/handler-runner/in-process-runner/aws-lambda-ric/UserFunction.js
Runtime.ImportModuleError: Error: Cannot find module 'index'
Require stack:
- /Users/dorianmazur/Documents/simple-serverless4-offline-example/node_modules/serverless-offline/src/lambda/handler-runner/in-process-runner/aws-lambda-ric/UserFunction.js
    at _loadUserApp (/Users/dorianmazur/Documents/simple-serverless4-offline-example/node_modules/serverless-offline/src/lambda/handler-runner/in-process-runner/aws-lambda-ric/UserFunction.js:310:15)
    at async module.exports.load (/Users/dorianmazur/Documents/simple-serverless4-offline-example/node_modules/serverless-offline/src/lambda/handler-runner/in-process-runner/aws-lambda-ric/UserFunction.js:341:21)
    at async InProcessRunner.run (file:///Users/dorianmazur/Documents/simple-serverless4-offline-example/node_modules/serverless-offline/src/lambda/handler-runner/in-process-runner/InProcessRunner.js:41:21)
    at async MessagePort.<anonymous> (file:///Users/dorianmazur/Documents/simple-serverless4-offline-example/node_modules/serverless-offline/src/lambda/handler-runner/worker-thread-runner/workerThreadHelper.js:24:14)
✖ Error: Cannot find module 'index'
Require stack:
- /Users/dorianmazur/Documents/simple-serverless4-offline-example/node_modules/serverless-offline/src/lambda/handler-runner/in-process-runner/aws-lambda-ric/UserFunction.js

You can reproduce this issue with the repository https://github.com/NicoHinderling/simple-serverless4-offline-example/tree/main Fixes #1800

Motivation and Context

Serverless v4 supports TypeScript out of the box. To resolve the module loading issue, I have updated the UserFunction file and integrated tsx to execute TypeScript files.

How Has This Been Tested?

Unit tests and manually tested the helloTypescript endpoint locally using the test suite located at tests/manual/nodejs