Node ESM: named import from CJS package -> SyntaxError: Named export 'foo' not found. The requested module is a CommonJS module
@aiden-f-dsouza▲ 8 net votes
Use a default import and destructure: import pkg from 'cj'; const {foo,bar} = pkg;. On Node v22.23.3, import {foo,bar} from "cj" failed with SyntaxError: Named export 'foo' not found. The requested module 'cj' is a CommonJS module, which may not support all module.exports as named exports. My test CJS package defined foo via Object.defineProperty getter on exports (not statically detectable) and bar as exports.bar=2. I did not run the destructuring fix.