Named imports stopped working from pure ESM projects starting in 0.2.39
See original GitHub issueI attempted to upgrade a project to the latest typeorm today, and I discovered the following issue with code that worked in 0.2.38.
import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm';
^^^^^^
SyntaxError: Named export 'Column' not found. The requested module 'typeorm' is a CommonJS module, which may not support all module.exports as named exports.
After some testing, I found that 0.2.39 was where this started. It seems that typeorm is the latest package to get hit by a problem introduced by TypeScript in version 4.4: the emitted CommonJS code for export * from './foo'
has changed, and Node.js no longer detects these exports for purposes of ESM / CommonJS compatibility. (It used to get emitted as tslib_1.__exportStar(require("./foo"), exports)
, but now it gets emitted as (0, tslib_1.__exportStar)(require("./foo"), exports)
)
Unfortunately this isn’t really something that Typeorm can directly fix. Either TypeScript needs to revert this change, or Node.js needs to fix their compatibility solution to recognize the new export syntax. Until one of those happens, would it be possible to compile typeorm with TypeScript version 4.3 or earlier? Alternatively, could an ESM entrypoint be added to restore named exports for ESM code?
Here are the relevant discussions on the issue in the TypeScript and Node repos:
https://github.com/nodejs/cjs-module-lexer/issues/63 https://github.com/microsoft/TypeScript/issues/45813
Issue Analytics
- State:
- Created 2 years ago
- Reactions:5
- Comments:7
I looked at typeorm-esm but was reluctant to actually try it since it seems unmaintained at the moment - last update was three years ago.
Unfortunately, we ended up reverting our project back to CommonJS for the moment. Typeorm was the second package that broke for us due to this TypeScript change, and we decided it was easier to downgrade the few packages we use that actually require ESM than to worry about this happening with minor/patch updates to any of our dependencies.
The good news is that the TypeScript issue has been fixed. It’ll obviously take some time for the fix to make it into a release, but once it does typeorm (and any other affected project) will be fixed as soon as they upgrade TypeScript. We’ll move back to ESM once that happens, since the risk of further breakage should be minimal.
For those wanting an easy “work around” I use a file
typeorm.ts
which I import from it instead.typeorm.ts