Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Emit ECMAScript Modules alongside CommonJS #6974

Closed
1 of 21 tasks
imnotjames opened this issue Oct 26, 2020 · 3 comments · Fixed by #8536
Closed
1 of 21 tasks

Emit ECMAScript Modules alongside CommonJS #6974

imnotjames opened this issue Oct 26, 2020 · 3 comments · Fixed by #8536

Comments

@imnotjames
Copy link
Contributor

Feature Description

The Problem

We do not emit ECMAScript modules - only CommonJS. As the support for ESM becomes larger
it will be more and more common to need to use features from ESM like named exports.

The Solution

As part of the process for building the CommonJS modules we should also emit a separate set of ECMAScript Modules.

This does not have to be a breaking change and is described in the NodeJS documention.

Adding something similar to this to the package.json could get us support for both:

{
  "type": "module",
  "main": "./index.js",
  "exports": {
    "import": "./esm/index.js",
    "require": "./index.js"
  }
}

Considered Alternatives

This could instead just be done as a breaking change - support only Node 12+ (or wherever ESM support started)
and just start emitting ESM. That's a pretty difficult set of changes and could break code for a number of people.

Additional Context

This came out of #6941

Relevant Database Driver(s)

  • aurora-data-api
  • aurora-data-api-pg
  • better-sqlite3
  • cockroachdb
  • cordova
  • expo
  • mongodb
  • mysql
  • nativescript
  • oracle
  • postgres
  • react-native
  • sap
  • sqlite
  • sqlite-abstract
  • sqljs
  • sqlserver

Are you willing to resolve this issue by submitting a Pull Request?

  • Yes, I have the time, and I know how to start.
  • Yes, I have the time, but I don't know how to start. I would need guidance.
  • No, I don't have the time, although I believe I could do it if I had the time...
  • No, I don't have the time and I wouldn't even know how to start.
@tomasvvv
Copy link

any updates regarding this so far?

@Sti2nd
Copy link
Contributor

Sti2nd commented Jul 16, 2021

TypeORM does not support ECMAScript Modules (ESM)!
Just wanted to write it explicitly for others to see as I spent some hours trying to get it to work, then I finally found this issue.

@robogeek
Copy link

robogeek commented Jan 2, 2022

I'm sorry .. but today, this attitude is no longer useful: "_ As the support for ESM becomes larger
it will be more and more common to need to use features from ESM like named exports._" As the author of books on Node.js programming, I started encouraging my readers to fully adopt ESM modules a long time ago.

I'm in the middle of updating one of my books, and have the intention of demonstrating full ESM adoption from TypeScript code, but have run into this problem when trying to use TypeORM.

In order to load the TypeORM decorators, I've tried this:

import * as TypeORM from "typeorm";
const Entity = TypeORM.Entity;
const Column = TypeORM.Column;
const PrimaryColumn = TypeORM.PrimaryColumn;
const ManyToMany = TypeORM.ManyToMany;
console.log(Entity);
console.log(TypeORM);
/* const { 
    Entity, 
    Column, 
    PrimaryColumn, 
    ManyToMany
} = TypeORM.default; */

But I get an error: TypeError: PrimaryColumn is not a function

The console.log statements show that TypeORM.default.PrimaryColumn does exist, but if I try to use that field from TypeScript, the compiler tells me there's no field named default.

I don't see a route forward. It seems the only solution is to inform my readers that TypeORM doesn't support use from ESM modules, despite ESM modules being the future.

JavaScript programmers will never get to enjoy the advantages of ESM modules if we don't take the steps necessary to get there. At this stage that means learning how to generate hybrid modules that support both ESM and CommonJS use.

skoshx added a commit to skoshx/typeorm that referenced this issue Jan 14, 2022
This feature adds esm output so typeorm works with modern bundlers

Closes: typeorm#6974
@skoshx skoshx mentioned this issue Jan 14, 2022
5 tasks
giladgd added a commit to giladgd/typeorm that referenced this issue Jan 17, 2022
giladgd added a commit to giladgd/typeorm that referenced this issue Jan 17, 2022
@giladgd giladgd mentioned this issue Jan 17, 2022
7 tasks
giladgd added a commit to giladgd/typeorm that referenced this issue Jan 17, 2022
pleerock pushed a commit that referenced this issue Jan 31, 2022
* feat: support importing TypeORM in esm projects

Closes: #6974
Closes: #6941

* bugfix: generate index.mjs directly out of commonjs exports

The new implementation generates ESM exports directly out of the commonjs exports, and provides a default export to maintain compatability with existing `import`s of the commonjs implementation

* feat: support loading ESM entity and connection config files

When TypeORM tries to load an entity file or a connection config file, it will determine what is the appropriate module system to use for the file and then `import` or `require` it as it sees fit.

Closes: #7516
Closes: #7159

* fix: adapt ImportUtils.importOrRequireFile tests to older version of nodejs

* fix: improved importOrRequireFile implementation

* feat: add solution to circular dependency issue in ESM projects

* docs: added FAQ regarding ESM projects

* chore: add `"type": "commonjs"` to package.json

* style

* docs: improve `ts-node` usage examples for CLI commands in ESM projects

* feat: add support for generating an ESM base project

* refactor: renamed `Related` type to `Relation`

* docs: added a section in the Getting Started guide regarding the `Relation` wrapper type in ESM projects

* docs: improved documentation of the `Relation` type

* docs: improved documentation of the `Relation` type

* docs: added ESM support to the list of TypeORM features
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants