I already knew the points in this thread by heart, but it still took me a while to realize my mistake, so I thought it would be cool to do a step-by-step guide to identify the problem... if anyone identifies another item that could generate this problem, edit the message or reply to it to add it. So here is "Everything that could be happening to generate the error No metadata found" (trying to make the list from the simplest to the most complex error):
- Check Entity File Extension in Configuration
- Ensure that entities in the configuration can read both .ts and .js files by setting it to entities:
[__dirname + '/../**/*.entity.{js,ts}']
.
- Verify Entity Paths
- Confirm that entity paths are correct, pointing to the dist folder if files are transpiled.
- Especially if you are using TypeScript, I recommend using the direct class declaration (this way you have more control over who is being referenced and when) instead of making the open declaration using masks like
*.ts
or *.js
;
- Include the entity in Module Imports
- In app.module.ts (or equivalent), include the entities explicitly, e.g., entities: [User].
3.1) Declare entity at forFeature
- The entity class must be present in the
forFeature
method call of the TypeOrmModule
class. If you are using NestJS with modules, this forFeature call must be present in an imports
attribute of the Module
decorator, either in the module file (e.g.: UserModule) or in the app module (e.g.: AppModule);
- Call initialize() on DataSource
- If using DataSource, ensure you call initialize() before accessing entities.If you are using an old typeorm version then you may consider use
connect
method after initilize
- Add @Entity() Decorator to Classes
- Check that the
@Entity()
decorator is present on all entity classes.
- Set
autoLoadEntities
to True
- In the configuration, add autoLoadEntities: true to automatically load entities.
- Check Database Connection Initialization
- Ensure the database connection is established before using entities, and
AppDataSource.initialize()
is awaited if necessary.
- Clear
dist/
Folder and Rebuild
- Clear the
dist/
folder, especially if using NestJS, to avoid outdated compiled files.
- Ensure Unique Entity Names
- Avoid duplicate entity names;
- Pay attention to files with mismatched casing, like
Billinginfo
vs. BillingInfo
.
- Avoid Conflicting Configuration Files
- Remove unnecessary files, like both
ormconfig.json
and ormconfig.ts
, to avoid confusion.
- If you project makes use of both try to stick with only one and if that is not possible be sure to be loading the correct one in the moment you are issuing the error while trying to use the entity;
- Verify database columns in entity class match table schema
- Check that the entity’s fields align with the database table's schema and that the entity declared name in the code is actually the same as the database repository.
- Use the correct DataSource
(this was the case I was running into this time)
- If you are accessing it through
QueryRunner
, check if it is declared in the data-source related to the QueryRunner. In the event that we have more than one data-source, it is common to use one QueryRunner to access some tables and another for other tables, so you may be using the wrong data-source. e.g. In my case I have logDataSource and dataSource, the class I was trying to use was at dataSource and I was using the logDataSource QueryRunner;
- Handle Migrations Properly in NestJS
- For migrations in NestJS, set them outside the module's options if using
TypeOrmModuleAsyncOptions
.
- Update TypeORM Version
- Sometimes, updating TypeORM to the latest version resolves compatibility issues.
Hope to help someone (or myself in the next time). Cheers