This error occurs when importing from an ES module that doesn't export the requested name. It commonly happens when trying to use a default import on a module that only has named exports, or when mixing ESM and CommonJS module systems.
This SyntaxError indicates a mismatch between what you're trying to import from a module and what that module actually exports. In ES modules, there are two types of exports: default exports and named exports. When you attempt to import something that doesn't exist in the module's export list, Node.js throws this error. The most common scenario is trying to use a default import (like `import something from './module'`) when the module only exports named exports (like `export { something }`). This also frequently occurs when importing from CommonJS modules in an ES module context, because CommonJS modules don't have true default exports - they only have `module.exports`. Node.js uses static analysis (cjs-module-lexer) to detect named exports from CommonJS modules for better interoperability, but this only works reliably when CommonJS modules use direct property assignments like `exports.name = value`, not when reassigning the entire `module.exports` object.
Open the file you're importing from and verify what it exports. Look for either:
Named exports:
// module.js
export function myFunction() { }
export const myValue = 42;Default export:
// module.js
export default function myFunction() { }If you see named exports, you must use named imports with curly braces. If you see a default export, use a default import without curly braces.
Adjust your import statement based on what the module exports:
For named exports, use curly braces:
import { myFunction, myValue } from './module.js';For default exports, don't use curly braces:
import myFunction from './module.js';For mixed exports:
import myDefault, { myNamed } from './module.js';If you're importing from a CommonJS module, use a default import and destructure:
Instead of this (will fail):
import { something } from './commonjs-module';Use this approach:
import pkg from './commonjs-module.js';
const { something } = pkg;Or use a namespace import:
import * as pkg from './commonjs-module.js';
pkg.something();If the module should have a default export but doesn't, add it:
Add a default export:
// module.js
function myFunction() {
// implementation
}
export default myFunction;Or export an object as default:
// module.js
const api = {
method1() { },
method2() { }
};
export default api;Ensure your package.json has the correct module configuration:
For ES modules, add:
{
"type": "module"
}Or use .mjs file extensions for ES modules without changing package.json.
For CommonJS (default), either omit "type" or use:
{
"type": "commonjs"
}If you have a mixed project, use explicit file extensions (.mjs for ESM, .cjs for CommonJS) instead of .js files.
ESM/CommonJS Interoperability:
Node.js uses static analysis to detect CommonJS named exports, but this only works when modules use direct property assignments (exports.name = value) rather than reassigning module.exports. When a CommonJS module uses module.exports = { ... }, only default imports will work from ESM.
Third-party pure ESM packages:
Some packages (like node-fetch v3+, chalk v5+) have migrated to pure ESM and no longer support CommonJS require(). If you're in a CommonJS project, you must either migrate to ESM, use dynamic import(), or downgrade to the last CommonJS-compatible version.
TypeScript considerations:
When using TypeScript with esModuleInterop: true, the compiler allows import something from 'module' for CommonJS modules, but this may fail at runtime in Node.js. Always verify the actual module format and use appropriate import syntax.
Bundler behavior:
Build tools like webpack, Rollup, and esbuild handle ESM/CommonJS differently. Some automatically add default exports or perform synthetic default imports. This can cause code that works during development to break in production or vice versa. Always test your builds in the actual runtime environment.
Error: Listener already called (once event already fired)
EventEmitter listener already called with once()
Error: EACCES: permission denied, open '/root/file.txt'
EACCES: permission denied
Error: Invalid encoding specified (stream encoding not supported)
How to fix Invalid encoding error in Node.js readable streams
Error: EINVAL: invalid argument, open
EINVAL: invalid argument, open
TypeError: readableLength must be a positive integer (stream config)
TypeError: readableLength must be a positive integer in Node.js streams