dherault / dherault/serverless-offline
serverless-offline lambdas break in node20.19 and node22 due to require(esm)
- Dominant language
- JavaScript
- Stars
- 5.3k
- Forks
- 811
- Avg merge
- 2d 4h
- Merged PRs (30d)
- 3
Description
## Bug Report
**Current Behavior**
When running serverless-offline in Node 22, and now Node 20.19, there is a startup error.
```
import { Server } from "@hapi/hapi"
^^^^^^
SyntaxError: Named export 'Server' not found. The requested module '@hapi/hapi' is a CommonJS module, which may not support all module.exports as named exports.
```
**Sample Code**
Running a basic serverless setup with a lambda function should trigger the issue.
- file: serverless.yml
```yaml
service: my-service
plugins:
- serverless-offline
provider:
runtime: nodejs18.x
stage: dev
functions:
hello:
events:
- http:
method: get
path: hello
handler: handler.hello
```
- file: handler.js
```js
"use strict"
const { stringify } = JSON
exports.hello = async function hello() {
return {
body: stringify({ foo: "bar" }),
statusCode: 200,
}
}
```
**Expected behavior/code**
**Environment**
- `serverless` version: 3.40
- `serverless-offline` version: 14.4
- `node.js` version: 20.19 or 22.x
- `OS`: macOS
**Possible Solution**
```
import hapi from "@hapi/hapi"
const { Server } = hapi;
```
Also, fs-extra, jsonschema, aws-lambda-ric/UserFunction.js. and possibly other commonjs packages that are not triggered by our code.
**Additional context/Screenshots**
The issue can be avoided by not upgrading node versions, or potentially engaging available node experimental flags, but updating the code to follow the now prescribed import-of-commonjs syntax seems appropriate.
Contributor guide
Research direction
Reproduce the startup failure with the serverless.yml and handler.js examples on Node 20.19 or 22, then trace the CommonJS imports involving @hapi/hapi, fs-extra, jsonschema, and aws-lambda-ric/UserFunction.js. Done means a basic lambda starts without the require(esm) error on the affected Node versions, with coverage for the failing loading path if the repository provides it.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript, node.js
- Domain
- backend, tooling
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100