serialize-error: Error require of ES Module not supported

Since 9.x (I also tried 10.x and 11.x) when importing serialize-error:

import { serializeError } from 'serialize-error';

I get the following error:

Error [ERR_REQUIRE_ESM]: require() of ES Module /Users/mick/git/myproject/node_modules/serialize-error/index.js from /Users/mick/myproject/src/myfile.ts not supported. Instead change the require of index.js in /Users/myproject/myfile.ts to a dynamic import() which is available in all CommonJS modules.

It works when building with tsc but not when starting with ts-node. Any idea what this could be? I dont find any require() somewhere…

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Reactions: 3
  • Comments: 21 (1 by maintainers)

Most upvoted comments

@sindresorhus could you please reopen as so many people are reporting the same issue?

Actually I stayed on 8.1.

We have 5+ Nest / TypeScript projects with hundreds of dependencies and this is the only one making problems and as I wrote above it is easy to reproduce. If you find anything please post it here, thanks!

Having the same problem. Downgrading to 8.1.0 helped.

“dependencies”: { “serialize-error”: “8.1.0”, “@types/express”: “^4.17.17”, “@types/jest”: “^29.5.2”, “@types/node”: “^20.1.0”, “jest”: “^29.5.0”, “ts-jest”: “^29.1.0”, “ts-node-dev”: “^2.0.0”, “typescript”: “^5.0.4” }

Like almost every sindresorhus/* package lately, this one also does not work with jest… Probably same problem. And while I understand why and that it is not a problem with the lib itself, the bottomline for me sadly is I won’t use it.

@papb The eval and string are necessary because TS will still transpile the code if: const { serializeError } = await import('serialize-error') is present. Which leads to the same ESM importing issue. In the SO comment I linked the post goes into it a bit deeper.

For as to why this module, in this place, in the specific point in time, localized in this specific repo. *shrug. Its an ESM module import issue for transpiling, I’m not sure if any of the other repo’s that I’ve installed are actually using ESM modules and this is just the first one in a long line of breaking dependencies, or if there is a specific bug present in ts-node for transpilation.

As far as I can see between the diff of the 8.1.0 release to the current 11.0.0 release. There isn’t anything obvious that is incorrect in the repo, and the only real change is exporting this as ESM. Which should be transpiled correctly but isn’t. Leading me to believe that its an issue in ts-node’s transpilation system. Which anecdotally (I don’t have the GH issues on hand) has varying levels of support and completeness.

I fussed around with it more and the following statement works for me instead of the normal imports in 11.x for serialize-error

            const { serializeError } = await (eval(`import('serialize-error')`) as Promise<
                typeof import('serialize-error')
            >);

The more I’ve dug into this the more I’m leaning toward this not being a library specific issue. Rather its an issue within the JS community about handling ESM modules appropriately.

I got the idea for that bit of code from the following SO: https://stackoverflow.com/questions/70545129/compile-a-package-that-depends-on-esm-only-library-into-a-commonjs-package

While not elegant I have a feeling until TS-Node offers support for transpiling to ESM this won’t be easily solved. Nor should we expect the library maintainer to fix it, since its not an issue with the library, rather the transpilation process from TS -> JS for ESM modules.

I have also rolled back to: "serialize-error": "^8.1.0" and this fixed the problem for me.

FWIW running: "@nestjs/core": "^9.2.1", Node: v14.21.2 "ts-node": "^10.0.0"