question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Named imports stopped working from pure ESM projects starting in 0.2.39

See original GitHub issue

I 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:open
  • Created 2 years ago
  • Reactions:5
  • Comments:7

github_iconTop GitHub Comments

2reactions
BenSjobergcommented, Dec 9, 2021

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.

1reaction
OmgImAlexiscommented, Jan 8, 2022

For those wanting an easy “work around” I use a file typeorm.ts which I import from it instead.

typeorm.ts
import TypeOrm from 'typeorm';

const {
    AbstractRepository,
    AdvancedConsoleLogger,
    AfterInsert,
    AfterLoad,
    AfterRemove,
    AfterUpdate,
    AlreadyHasActiveConnectionError,
    Any,
    BaseEntity,
    BeforeInsert,
    BeforeRemove,
    BeforeUpdate,
    Between,
    Binary,
    Brackets,
    CannotAttachTreeChildrenEntityError,
    CannotConnectAlreadyConnectedError,
    CannotCreateEntityIdMapError,
    CannotDetermineEntityError,
    CannotExecuteNotConnectedError,
    CannotGetEntityManagerNotConnectedError,
    CannotReflectMethodParameterTypeError,
    ChangeStream,
    Check,
    ChildEntity,
    CircularRelationsError,
    Code,
    Column,
    ColumnTypeUndefinedError,
    Connection,
    ConnectionIsNotSetError,
    ConnectionManager,
    ConnectionNotFoundError,
    ConnectionOptionsReader,
    CreateDateColumn,
    Cursor,
    CustomRepositoryCannotInheritRepositoryError,
    CustomRepositoryDoesNotHaveEntityError,
    CustomRepositoryNotFoundError,
    DataTypeNotSupportedError,
    Db,
    DefaultNamingStrategy,
    DeleteDateColumn,
    DeleteQueryBuilder,
    DeleteResult,
    Double,
    DriverOptionNotSetError,
    DriverPackageNotInstalledError,
    Entity,
    EntityColumnNotFound,
    EntityManager,
    EntityMetadata,
    EntityMetadataNotFoundError,
    EntityNotFoundError,
    EntityRepository,
    EntitySchema,
    Equal,
    EventSubscriber,
    Exclusion,
    FileLogger,
    FindOperator,
    FindOptionsUtils,
    FindRelationsNotFoundError,
    Generated,
    GridFSBucket,
    GridFSBucketReadStream,
    GridFSBucketWriteStream,
    ILike,
    In,
    Index,
    InitializedRelationError,
    InsertQueryBuilder,
    InsertResult,
    InsertValuesMissingError,
    IsNull,
    JoinColumn,
    JoinTable,
    LessThan,
    LessThanOrEqual,
    Like,
    LimitOnUpdateNotSupportedError,
    LockNotSupportedOnGivenDriverError,
    Long,
    ManyToMany,
    ManyToOne,
    MaxKey,
    MetadataAlreadyExistsError,
    MetadataWithSuchNameAlreadyExistsError,
    Migration,
    MigrationExecutor,
    MinKey,
    MissingDeleteDateColumnError,
    MissingDriverError,
    MissingJoinColumnError,
    MissingJoinTableError,
    MissingPrimaryColumnError,
    MongoClient,
    MongoEntityManager,
    MongoError,
    MongoRepository,
    Mongos,
    MoreThan,
    MoreThanOrEqual,
    MssqlParameter,
    MustBeEntityError,
    NamingStrategyNotFoundError,
    NoConnectionForRepositoryError,
    NoConnectionOptionError,
    NoNeedToReleaseEntityManagerError,
    NoVersionOrUpdateDateColumnError,
    Not,
    ObjectID,
    ObjectIdColumn,
    OffsetWithoutLimitNotSupportedError,
    OneToMany,
    OneToOne,
    OptimisticLockCanNotBeUsedError,
    OptimisticLockVersionMismatchError,
    PersistedEntityNotFoundError,
    PessimisticLockTransactionRequiredError,
    PrimaryColumn,
    PrimaryColumnCannotBeNullableError,
    PrimaryGeneratedColumn,
    QueryBuilder,
    QueryFailedError,
    QueryResult,
    QueryRunnerAlreadyReleasedError,
    QueryRunnerProviderAlreadyReleasedError,
    Raw,
    ReadPreference,
    RelationId,
    RelationQueryBuilder,
    ReplSet,
    Repository,
    RepositoryNotFoundError,
    RepositoryNotTreeError,
    ReturningStatementNotSupportedError,
    SelectQueryBuilder,
    Server,
    SimpleConsoleLogger,
    SubjectRemovedAndUpdatedError,
    SubjectWithoutIdentifierError,
    Table,
    TableCheck,
    TableColumn,
    TableExclusion,
    TableForeignKey,
    TableIndex,
    TableInheritance,
    TableUnique,
    Timestamp,
    Transaction,
    TransactionAlreadyStartedError,
    TransactionManager,
    TransactionNotStartedError,
    TransactionRepository,
    Tree,
    TreeChildren,
    TreeLevelColumn,
    TreeParent,
    TreeRepository,
    TreeRepositoryNotSupportedError,
    TypeORMError,
    Unique,
    UpdateDateColumn,
    UpdateQueryBuilder,
    UpdateResult,
    UpdateValuesMissingError,
    UsingJoinColumnIsNotAllowedError,
    UsingJoinColumnOnlyOnOneSideAllowedError,
    UsingJoinTableIsNotAllowedError,
    UsingJoinTableOnlyOnOneSideAllowedError,
    VersionColumn,
    ViewColumn,
    ViewEntity,
    createConnection,
    createConnections,
    createQueryBuilder,
    getConnection,
    getConnectionManager,
    getConnectionOptions,
    getCustomRepository,
    getFromContainer,
    getManager,
    getMetadataArgsStorage,
    getMongoManager,
    getMongoRepository,
    getRepository,
    getSqljsManager,
    getTreeRepository,
    useContainer
} = TypeOrm;

export {
    AbstractRepository,
    AdvancedConsoleLogger,
    AfterInsert,
    AfterLoad,
    AfterRemove,
    AfterUpdate,
    AlreadyHasActiveConnectionError,
    Any,
    BaseEntity,
    BeforeInsert,
    BeforeRemove,
    BeforeUpdate,
    Between,
    Binary,
    Brackets,
    CannotAttachTreeChildrenEntityError,
    CannotConnectAlreadyConnectedError,
    CannotCreateEntityIdMapError,
    CannotDetermineEntityError,
    CannotExecuteNotConnectedError,
    CannotGetEntityManagerNotConnectedError,
    CannotReflectMethodParameterTypeError,
    ChangeStream,
    Check,
    ChildEntity,
    CircularRelationsError,
    Code,
    Column,
    ColumnTypeUndefinedError,
    Connection,
    ConnectionIsNotSetError,
    ConnectionManager,
    ConnectionNotFoundError,
    ConnectionOptionsReader,
    CreateDateColumn,
    Cursor,
    CustomRepositoryCannotInheritRepositoryError,
    CustomRepositoryDoesNotHaveEntityError,
    CustomRepositoryNotFoundError,
    DataTypeNotSupportedError,
    Db,
    DefaultNamingStrategy,
    DeleteDateColumn,
    DeleteQueryBuilder,
    DeleteResult,
    Double,
    DriverOptionNotSetError,
    DriverPackageNotInstalledError,
    Entity,
    EntityColumnNotFound,
    EntityManager,
    EntityMetadata,
    EntityMetadataNotFoundError,
    EntityNotFoundError,
    EntityRepository,
    EntitySchema,
    Equal,
    EventSubscriber,
    Exclusion,
    FileLogger,
    FindOperator,
    FindOptionsUtils,
    FindRelationsNotFoundError,
    Generated,
    GridFSBucket,
    GridFSBucketReadStream,
    GridFSBucketWriteStream,
    ILike,
    In,
    Index,
    InitializedRelationError,
    InsertQueryBuilder,
    InsertResult,
    InsertValuesMissingError,
    IsNull,
    JoinColumn,
    JoinTable,
    LessThan,
    LessThanOrEqual,
    Like,
    LimitOnUpdateNotSupportedError,
    LockNotSupportedOnGivenDriverError,
    Long,
    ManyToMany,
    ManyToOne,
    MaxKey,
    MetadataAlreadyExistsError,
    MetadataWithSuchNameAlreadyExistsError,
    Migration,
    MigrationExecutor,
    MinKey,
    MissingDeleteDateColumnError,
    MissingDriverError,
    MissingJoinColumnError,
    MissingJoinTableError,
    MissingPrimaryColumnError,
    MongoClient,
    MongoEntityManager,
    MongoError,
    MongoRepository,
    Mongos,
    MoreThan,
    MoreThanOrEqual,
    MssqlParameter,
    MustBeEntityError,
    NamingStrategyNotFoundError,
    NoConnectionForRepositoryError,
    NoConnectionOptionError,
    NoNeedToReleaseEntityManagerError,
    NoVersionOrUpdateDateColumnError,
    Not,
    ObjectID,
    ObjectIdColumn,
    OffsetWithoutLimitNotSupportedError,
    OneToMany,
    OneToOne,
    OptimisticLockCanNotBeUsedError,
    OptimisticLockVersionMismatchError,
    PersistedEntityNotFoundError,
    PessimisticLockTransactionRequiredError,
    PrimaryColumn,
    PrimaryColumnCannotBeNullableError,
    PrimaryGeneratedColumn,
    QueryBuilder,
    QueryFailedError,
    QueryResult,
    QueryRunnerAlreadyReleasedError,
    QueryRunnerProviderAlreadyReleasedError,
    Raw,
    ReadPreference,
    RelationId,
    RelationQueryBuilder,
    ReplSet,
    Repository,
    RepositoryNotFoundError,
    RepositoryNotTreeError,
    ReturningStatementNotSupportedError,
    SelectQueryBuilder,
    Server,
    SimpleConsoleLogger,
    SubjectRemovedAndUpdatedError,
    SubjectWithoutIdentifierError,
    Table,
    TableCheck,
    TableColumn,
    TableExclusion,
    TableForeignKey,
    TableIndex,
    TableInheritance,
    TableUnique,
    Timestamp,
    Transaction,
    TransactionAlreadyStartedError,
    TransactionManager,
    TransactionNotStartedError,
    TransactionRepository,
    Tree,
    TreeChildren,
    TreeLevelColumn,
    TreeParent,
    TreeRepository,
    TreeRepositoryNotSupportedError,
    TypeORMError,
    Unique,
    UpdateDateColumn,
    UpdateQueryBuilder,
    UpdateResult,
    UpdateValuesMissingError,
    UsingJoinColumnIsNotAllowedError,
    UsingJoinColumnOnlyOnOneSideAllowedError,
    UsingJoinTableIsNotAllowedError,
    UsingJoinTableOnlyOnOneSideAllowedError,
    VersionColumn,
    ViewColumn,
    ViewEntity,
    createConnection,
    createConnections,
    createQueryBuilder,
    getConnection,
    getConnectionManager,
    getConnectionOptions,
    getCustomRepository,
    getFromContainer,
    getManager,
    getMetadataArgsStorage,
    getMongoManager,
    getMongoRepository,
    getRepository,
    getSqljsManager,
    getTreeRepository,
    useContainer
};
Read more comments on GitHub >

github_iconTop Results From Across the Web

Unable of get running a pure ESM node project - Stack Overflow
CommonJS has a terriable API for modules, and using the official ECMAScript Modules imports, exports, URIs File-urls etc, makes it all worth it, ......
Read more >
Getting Started with (and Surviving) Node.js ESM
The Node.js ecosystem has already started shifting towards ESM, ... is that you can only do a default object import and not named...
Read more >
API - esbuild
The esm format stands for "ECMAScript module". It assumes the environment supports import and export syntax. Entry points with exports in CommonJS module ......
Read more >
Documentation - ECMAScript Modules in Node.js - TypeScript
js has been working to support running ECMAScript modules (ESM). This has been a very difficult feature to support, since the foundation of...
Read more >
Python import: Advanced Techniques and Tips
However, let's get started with the basics: importing modules and packages. ... In particular, your top-level module and package names should be unique....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found