dherault / serverless-offline

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

Fix ReferenceError: Headers is not defined #1768

Open cmeadows opened 4 months ago

cmeadows commented 4 months ago

Description

When locally Invoking functions via serverless-offline (cognito events, specifically), I received ReferenceError: Headers is not defined. This is being caused by the Fetch API Headers class and potentially missing polyfill for the API in my Node environment (specifically v20).

As such, I have added node-fetch to bring Fetch API compatibility.

Motivation and Context

When using serverless-offline and running local Invoke within a Python environment, I was getting the following error:

Debug: internal, implementation, error 
    ReferenceError: Headers is not defined
    at handler (file:///Users/collinmeadows/Development/myapp/node_modules/serverless-offline/src/lambda/routes/invocations/invocationsRoute.js:18:29)
    at exports.Manager.execute (/Users/collinmeadows/Development/myapp/node_modules/@hapi/hapi/lib/toolkit.js:57:29)
    at Object.internals.handler (/Users/collinmeadows/Development/myapp/node_modules/@hapi/hapi/lib/handler.js:46:48)
    at exports.execute (/Users/collinmeadows/Development/myapp/node_modules/@hapi/hapi/lib/handler.js:31:36)
    at Request._lifecycle (/Users/collinmeadows/Development/myapp/node_modules/@hapi/hapi/lib/request.js:370:68)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async Request._execute (/Users/collinmeadows/Development/myapp/node_modules/@hapi/hapi/lib/request.js:280:9)

How Has This Been Tested?

I am actively running serverless-offline within multiple stacks. I made this change locally within node_modules/serverless-offline and re-invoked the function with no errors.