[Lambda/NodeJS] Lambda Stop Working After Updating to Node.js 20

1. Problem If you are using AWS Lambda and update your Runtime to Node.js 20.x, you may see the error as follows: Response { "errorType": "ReferenceError", "errorMessage": "require is not defined in ES module scope, you can use import instead", "trace": [ "ReferenceError: require is not defined in ES module scope, you can use import instead", " at file:///var/task/index.mjs:2:68", " at ModuleJob.run (node:internal/modules/esm/module_job:234:25)", " at async ModuleLoader.import (node:internal/modules/esm/loader:473:24)", " at async _tryAwaitImport (file:///var/runtime/index.mjs:1008:16)", " at async _tryRequire (file:///var/runtime/index.mjs:1057:86)", " at async _loadUserApp (file:///var/runtime/index.mjs:1081:16)", " at async UserFunction.js.module.exports.load (file:///var/runtime/index.mjs:1119:21)", " at async start (file:///var/runtime/index.mjs:1282:23)", " at async file:///var/runtime/index.mjs:1288:1" ] } This is because Node.js 20.x uses ECMAScript modules (ESM) by default. You need to update your code to use import instead of require. ...

DigitalOcean Referral Badge
Sign up to get $200, 60-day account credit !