TypeORM cannot find an entity, or the metadata for a relation
Entity metadata for Role#users was not found.Short answer
TypeORM only knows the entities listed in the entities option of the DataSource that is running. If a class, or a relation target, is missing from it, you get a missing-metadata error. Register every entity as a class or with a glob that matches the files you actually run: .ts under ts-node, .js after a build.
Fix it fast
1Find out which cause you have
Print the entities the DataSource actually loaded. If the class from the error is not in the list, the cause is below.
await AppDataSource.initialize() console.log(AppDataSource.entityMetadatas.map((m) => m.name))2Match what you see, then jump to its fix
What does the error look like?
Entity metadata for Role#users was not found.
Cause 1 of 2
The entity is not listed in the DataSource options
1Check
The docs say each entity must be registered in the data source options. A related entity, such as the other side of a relation, needs to be there too.
grep -n -A6 "entities" src/data-source.ts2Fix
Add the class, or a glob that covers its file, to entities.
import { User } from "./entities/User"
import { Role } from "./entities/Role"
export const AppDataSource = new DataSource({
// ...
entities: [User, Role],
})3Verify
Run the first-step print again; both classes appear.
npx ts-node src/check-entities.tsConfirmed by typeorm.io
Cause 2 of 2
The glob matches .ts files but the app runs compiled .js, or the other way round
1Check
The data source docs show a pattern that ends in {.js,.ts} so that both modes work. A pattern that names only src/**/*.ts finds nothing inside dist/.
ls dist/entities 2>/dev/null
grep -n "entities" src/data-source.ts2Fix
Use a path based on __dirname with both extensions, so it follows wherever the file runs from.
entities: [__dirname + "/entities/**/*{.ts,.js}"],3Verify
Start the app from source and from the build; the entity list is the same.
npx ts-node src/check-entities.ts
node dist/check-entities.jsConfirmed by typeorm.io
How do I stop it coming back?
- List entity classes explicitly in small projects; it removes glob mistakes.
- Keep one DataSource definition and import it everywhere, including the CLI.
- Add a startup log that prints the number of loaded entities.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- WorkforceA field app, an admin console and a self-hosted Node.js server for push and reconciliation.
Related fixes
Back end
TypeORM connects but creates no tables or columns
A new column is missing, with no error at startup
TypeORM does not create tables by itself.
3 causes, each with a fixBack end
TypeScript cannot find module '@nestjs/core' or its type declarations
error TS2307: Cannot find module '@nestjs/common' or its corresponding type declarations.
TS2307 means TypeScript searched node_modules and found neither the package nor its types.
3 causes, each with a fixBack end
NestJS app will not start: Cannot find module '/app/dist/main'
Cannot find module 'dist/main'
Node cannot find dist/main.js because the build did not put it there.
4 causes, each with a fixBack end
TypeError: something.default is not a constructor in a NestJS app
Exception has occurred: TypeError: xxx.default is not a constructor
TypeScript turns import X from 'pkg' into require('pkg').default when esModuleInterop is off, and a CommonJS package has no default, so new X() fails.
2 causes, each with a fixBack end
TypeScript cannot find req.cookies in a NestJS controller
error TS2339: Property 'cookies' does not exist on type
TypeScript says 'cookies' is missing because the Request type in your file is not Express's, or because cookie support is not installed.
3 causes, each with a fixBack end
TypeORM warns that createConnection is deprecated
'createConnection' is deprecated.
TypeORM replaced Connection with DataSource.
2 causes, each with a fix