Skip to content

serverless-offline lambdas break in node20.19 and node22 due to require(esm) #1854

@smundro

Description

@smundro

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
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
"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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions