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

'typeorm' does not provide an export named 'Column' #6941

Closed
TheMutabor opened this issue Oct 20, 2020 · 10 comments · Fixed by #8536
Closed

'typeorm' does not provide an export named 'Column' #6941

TheMutabor opened this issue Oct 20, 2020 · 10 comments · Fixed by #8536
Assignees

Comments

@TheMutabor
Copy link

TheMutabor commented Oct 20, 2020

Issue type:

[X] bug report
[ ] feature request
[ ] documentation issue

Database system/driver:

[ ] cordova
[ ] mongodb
[ ] mssql
[X] mysql / mariadb
[ ] oracle
[ ] postgres
[ ] cockroachdb
[ ] sqlite
[ ] sqljs
[ ] react-native
[ ] expo

TypeORM version:

[X] latest
[ ] @next
[ ] 0.x.x (or put your version here)

Steps to reproduce or a small repository showing the problem:
If i initialize my MySQL Connection over typeorm with my Accounts entityi get this error:

import { BaseEntity, Column, Entity, PrimaryGeneratedColumn } from 'typeorm';
                     ^^^^^^
SyntaxError: The requested module 'typeorm' does not provide an export named 'Column'

my Accounts entity

import { BaseEntity, Column, Entity, PrimaryGeneratedColumn } from 'typeorm';
@Entity({ name: 'accounts' })
export class Accounts extends BaseEntity {
    @PrimaryGeneratedColumn()
    id: number;

    @Column({ type: 'varchar' })
    username: string;

    @Column({ type: 'varchar' })
    password: string;

    @Column({ type: 'varchar' })
    hardwareID: string;

    @Column({ type: 'int' })
    maxCharacter: number;
} 
@imnotjames
Copy link
Contributor

Can you create a repository that exhibits this? I can't replicate

@TheMutabor
Copy link
Author

TheMutabor commented Oct 20, 2020

Can you create a repository that exhibits this? I can't replicate

i don´t come that far. I got this error when i`m trying to creat a connection with this function:

import orm from 'typeorm';
import { Accounts } from './entity/accounts.js';
const SQLConfig = {
    type: 'mysql',
    host: 'localhost',
    port: 3306,
    username: 'xxx',
    password: 'xxx',
    database: 'xxx',
    synchronize: true,
    logging: false,
    entities: [Accounts],
};

var instance = null;

export default class Connection {
    constructor() {

        if (!instance) {
            orm.createConnection(SQLConfig)
                .then((con) => {
                    this.connection = con;
                    instance = this;
                })
                .catch((err) => {
                    throw err;
                });
        }

        return instance;
    }
}

And if i call this:

import SQL from './database/mysql.js';
let db = new SQL();

i get the error

import { BaseEntity, Column, Entity, PrimaryGeneratedColumn } from 'typeorm';
                     ^^^^^^
SyntaxError: The requested module 'typeorm' does not provide an export named 'Column'

@imnotjames
Copy link
Contributor

Ok - I still am unable to replicate. Can you please create a GitHub repository that includes code which I can npm install and then compile which exhibits this issue?

@TheMutabor
Copy link
Author

I can try, but this script i made is a game resource for a Alt:V Server and the js script get executed by the Server

@imnotjames
Copy link
Contributor

If you can create a minimal repo that reproduces the issue it'd be best but whatever you can get would help with finding the root cause.

@TheMutabor
Copy link
Author

i can provide you the full repo. with alle Server Files it`s allready on Github but private, let me try to give you rights to it

@TheMutabor
Copy link
Author

If you can create a minimal repo that reproduces the issue it'd be best but whatever you can get would help with finding the root cause.

I inivted you to a Git Repo there with the hole Projekt.
in the "src" folder you will find alle uncompiled .ts files and also all resources
in the "resources" files are the compiled .js files this is what get executed by the altv-server.exe
the relevant files are the config file for typeORM under: "resources\SectorV\server\config\database.js" and the mysql class witch initialize the typeorm here: "resources\SectorV\server\database\mysql.js" here you will also find in a sub folder the entity. The mysql.js get called from the "resources\SectorV\server\startup.js" this file get executed when the altv-server.exe starts

@TheMutabor
Copy link
Author

Sorry, i`m quite new in Node JS/TypeScript things so the Project Structure may be a bit wyred 🙉 I wasn't prepared for visitors

@imnotjames imnotjames self-assigned this Oct 21, 2020
@imnotjames
Copy link
Contributor

Hey there - sorry for taking a while to get to this:

  • I cloned the repo
  • I erased altv-server.exe & ran npm run update to grab the linux version of the server
  • I erased node_modules & ran npm install to refresh dependencies
  • I ran npm run build

I'm pretty sure this is some wonk that exists between how Typescript handles module resolution & how nodejs does.

You're currently using ESM because type: "module" is defined in your package.json.

When importing CommonJS modules, the module.exports object is provided as the default export. Named exports may be available, provided by static analysis as a convenience for better ecosystem compatibility.

TypeORM corrently doesn't exactly export ESM - it exports commonjs modules. And if static analysis can get you to named exports... great! Except.. when it can't. Like now.

There are two ways you can work around this:

  • Switch your project to have "type": "commonjs" in your package.json & update your tsconfig.json to emit commonjs modules
  • Use the default export provided by TypeORM - import TypeORM from "typeorm"; @TypeORM.Entity();class Foo {}

There's ONE way that TypeORM could change to help with this as well - export ESM as well as commonJS! I'll think about adding this but there's a lot of other things I think might be more important.. 🤷

@imnotjames
Copy link
Contributor

In short - this is an environment issue - and as such, I'll be closing it out. However, for more support on the matter -

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
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants