typeorm: ts-node throwing RepositoryNotFoundError

Here is the example repo. https://github.com/zackarychapple/flagger

When I am starting the server with nodemon everything initializes correctly however when seed tries to run it bombs out with the below error. I tried several paths for mapping to my entities however it seems none of them pick up. Any thoughts?

(node:99829) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): RepositoryNotFoundError: No repository for "FlagsEntity" was found. Looks like this entity is not registered in current "default" connection?
(node:99829) DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 18 (3 by maintainers)

Most upvoted comments

I also had the same No repository for ... in current "default" connection and it worked fine for development (running with nodemon), but failed after tsc compilation running under node. I had to change:

entities: [
          __dirname + '/../**/**.entity.ts'
 ],

to:

entities: [
          __dirname + '/../**/**.entity{.ts,.js}'
 ],

I see a reference to this in the typeorm docs, is this the prescribed way for dealing with entities in typescript? (https://typeorm.github.io/connection.html#creating-connection)

no, it depends how you run your app. If you are running via ts-node then you can use “.ts” files as entities. If you aren’t using ts-node then you must specify “.js” files. And you need to make sure to specify proper output directory (if you are using outDir tsc option) to ensure paths to your generated js files are correct.

entities: [
        // any entity file under src/modules
        __dirname + '/*.entity.ts'
],

I can’t find any *.entity.ts in the folder where this file (flags.database.config.ts) exists. It cannot grab your entities if the files they live in do not match the pattern you’ve specified.

It worked for me:

const connectionOptions: ConnectionOptions = {
    name: 'default',
    type: 'mysql',
    port: 3306,
    synchronize: true,
    logging: true,
    host: 'localhost',
    username: 'username',
    database: 'database',
    password: '',
    entities: [
        __dirname + '/entity/*{.ts,.js}'
    ]
}
createConnection(connectionOptions).then(async connection => {...})

@chriszrc Prove me wrong but I think it worked when I totally omitted the file extension and simply wrote

entities: [
    `${__dirname}/**/*.entity`,
]

@apologetics you’re right. The globber was not matching what I expected. __dirname + '/../*/*.entity.ts' is what got it to work for me. Thank you for your help.

first, you don’t need TypeOrmDatabaseService extra wrapper - typeorm already has everything you do and you just create extra redundant layer. Second, better if you’ll use single ormconfig.json file and store your connection settings there. Third, create connection in your app bootstrap file, in a place like server.ts.

And regarding your problem - as @apologetics told you the problem in __dirname + '/*.entity.ts' - it does not point to any file, make sure your path are really point to entities.

Finally I can use official nest new project’s npm run start:dev and npm run build > cd dist > node main after change the ormconfig.js

"entities": [
    __dirname + '/../**/**.entity.js'
 ],

no SyntaxError: Unexpected token import and no Looks like this entity is not registered in current ...

i found error it says that cannot find name __dirname what is that means?