I have been working on a typescript express app for a while, and I wanted to deploy with serverless as I have done in the past with other projects. I converted the app to serverless with the cli, and modified the index.ts and serverless.yml to match my configuration.
The app starts up fine but as soon as I make a call to one of the endpoints I get the following error:
✖ Unhandled exception in handler 'api'. ✖ Error: Dynamic require of "fs" is not supported at file:///Users/user/development/projects/diligence/prod/diligence_backend/.serverless/build/src/index.js:11:9 at node_modules/dotenv/lib/main.js (/Users/user/development/projects/diligence/prod/diligence_backend/node_modules/dotenv/lib/main.js:1:12) at __require2 (file:///Users/user/development/projects/diligence/prod/diligence_backend/.serverless/build/src/index.js:17:50) at <anonymous> (/Users/user/development/projects/diligence/prod/diligence_backend/node_modules/dotenv/config.js:2:3) at <anonymous> (/Users/user/development/projects/diligence/prod/diligence_backend/node_modules/dotenv/config.js:9:1) at ModuleJob.run (node:internal/modules/esm/module_job:222:25) at async ModuleLoader.import (node:internal/modules/esm/loader:323:24) at async _tryAwaitImport (/Users/user/development/projects/diligence/prod/diligence_backend/node_modules/serverless-offline/src/lambda/handler-runner/in-process-runner/aws-lambda-ric/UserFunction.js:94:12) at async _tryRequire (/Users/user/development/projects/diligence/prod/diligence_backend/node_modules/serverless-offline/src/lambda/handler-runner/in-process-runner/aws-lambda-ric/UserFunction.js:182:22) at async _loadUserApp (/Users/user/development/projects/diligence/prod/diligence_backend/node_modules/serverless-offline/src/lambda/handler-runner/in-process-runner/aws-lambda-ric/UserFunction.js:236:12) ✖ Dynamic require of "fs" is not supported It seems like the error has something to do with dotenv, and the fs library, but I am not actually using the fs library myself. I have a basic serverless.yml setup as follows.
service: diligence-backend stages: default: params: tableName: "users-table-${sls:stage}" provider: name: aws runtime: nodejs20.x iam: role: statements: - Effect: Allow Action: - dynamodb:Query - dynamodb:Scan - dynamodb:GetItem - dynamodb:PutItem - dynamodb:UpdateItem - dynamodb:DeleteItem Resource: - Fn::GetAtt: [UsersTable, Arn] functions: api: handler: src/index.handler events: - httpApi: "*" plugins: - serverless-offline useDotenv: true I am currently running this locally with the serverless-offline package. The main difference between this project and my other working serverless project is that this one is typescript so I’m not sure if that is affecting things. If anyone has any ideas let me know. Thanks!