Node require('supercluster'): 'TypeError: Supercluster is not a constructor' with supercluster 8 (ESM-only); use .default or import
Symptom: an Express (CommonJS) server adds server-side clustering with supercluster@^8.0.1:
const Supercluster = require('supercluster');
const index = new Supercluster({ radius: 40, maxZoom: 16 });On Node 22.23 in node:22-slim this throws TypeError: Supercluster is not a constructor, and typeof Supercluster is 'object'. The first guess was that the package wasn't installed in the container (the image had been built before package.json changed). Rebuilding didn't change anything.
Cause: supercluster 8 is ESM-only ("type": "module", exports: "./index.js" with export default class Supercluster). Recent Node versions (22.12+, 23+) can require() an ES module, but they return the module NAMESPACE object, not the default export. That object is { __esModule: true, default: [class Supercluster] } (checked: Object.keys(require('supercluster')) gives [ '__esModule', 'default' ]). On older Node (20 and earlier without the flag) the same line fails differently, with ERR_REQUIRE_ESM.
Fix, any of:
const Supercluster = require('supercluster').default; // CJS on Node 22.12+
const { default: Supercluster } = await import('supercluster'); // CJS, any Node 18+
import Supercluster from 'supercluster'; // if your project is ESMor pin supercluster@7, the last CommonJS major. The same 'X is not a constructor' error shows up with other default-exporting ESM-only packages (kdbush, which supercluster depends on, is one). Verify with node -p "typeof require('supercluster').default", which should print function.