{

Fix for EntityMetadataNotFound: No metadata was found in typeorm


In the NestJS application, Created a Custom Entity and corresponding Controller, Service, and started an application, got the below error during startup.

EntityMetadataNotFound: No metadata for “entity” was found

Let’s see how to fix this error.

Solution for EntityMetadataNotFound: No metadata for was found

The entity is a Typescript class that maps to a table in the database.

If you defined the entities with typeorm decorator for your project, This error occurs while loading all entities which pointed to the incorrect directory with ts files.

NestJS expects the entities from the output directory of Js files. Please make sure that provide an entities path with .js and .ts files Listed all possible fixes.

  • First-way, Make sure that your typescript entity classes should have a @Entity() decorator.

For example, the employee class contains all fields Class must be added with @Entity() decorator

import { Entity, Column, PrimaryGeneratedColumn } from 'typeorm';

@Entity()
export class Employee {
    @PrimaryGeneratedColumn()
    id: number;

    @Column()
    firstName: string;

    @Column()
    lastName: string;

    @Column()
    email: string;

    @Column()
    phone: string;

    @Column()
    salary: number;

    @Column()
    isactive: boolean;
}

Second-way, Make sure that you are providing an entities value with a path to entities to TypeOrmModule.forRoot().

Example The below code contains entities attribute values pointed to all entity files in the directory. You can also define single entity names like entities:[entity]

@Module({
    imports: [
      TypeOrmModule.forRoot({
       entities: [__dirname + '/../**/*.entity.{js,ts}']
      }),
      ...
    ],
})

In case, If you are using ormconfig.js file

import { DataSourceOptions } from 'typeorm';

const config: DataSourceOptions = {
    "type": "mysql",
    "host": "localhost",
    "port": 3306,
    "username": "root",
    "password": "",
    "database": "test",
    "entities": [
        "/**/*.entity{.ts,.js}"
    ],
    "synchronize": true,
}
export default config;

the third way, enable autoLoadEntities to be true in the configuration object as given below

@Module({
    imports: [
      TypeOrmModule.forRoot({
         autoLoadEntities: true
      }),
      ...
    ],
})
THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
Subscribe
You'll get a notification every time a post gets published here.