EntityMetadataNotFound: No metadata for "Task" was found - NestJS

Viewed 11497

I am learning the NestJS course from Udemy https://www.udemy.com/course/nestjs-zero-to-hero.

And I am stuck with a strange issue and I have tried many things but nothing seems to be working. Here is the issue and complete code that I have.

Error that I am getting

Error that I am getting

My ORM configuration file:

ORM Configuration file

**Task Entity File: ** enter image description here

Finally I am importing the configuration file in tasks.module.ts file enter image description here

People facing there issue have resolved it with different fixes,

  1. Some said that we might be adding misspelled file name or path in configuration that might have caused this issue.
  2. Some said that changing from npm to yarn has fixed the issue.
  3. And few also said that the issue is with ORM itself.

I have tried all possible solutions that are available over the internet but was not able to fix this. Its been quite few days now and I am looking for a helping hand or savior on stack overflow.

Meanwhile, I will try to see a few more possibilities that could help but if you have faced this issue do let me know the possible solutions.

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

to

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

in typeorm.config.ts

it works on me

In NestJS adding .js along side .ts worked for me

typeorm.config.ts

Before

import { TypeOrmModuleOptions } from '@nestjs/typeorm';

export const TypeORMConfig: TypeOrmModuleOptions = {
  type: 'postgres',
  url: process.env.DATABASE_URL,
  synchronize: true,
  entities: [__dirname + '/../**/*.entity.ts'],
  migrationsTableName: 'Migrations_History',
};

After

import { TypeOrmModuleOptions } from '@nestjs/typeorm';

export const TypeORMConfig: TypeOrmModuleOptions = {
  type: 'postgres',
  url: process.env.DATABASE_URL,
  synchronize: true,
  entities: [__dirname + '/../**/*.entity{.ts,.js}'],
  migrationsTableName: 'Migrations_History',
};

You're missing the @Entity() decorator on your Task class. This sets the metadata that TypeORM is looking for according to its docs.

I have faced the same issue. After trying all the other mentioned solutions here which they do not worked for me, I have modified the typeorm.config.ts file by adding the following option:

  autoLoadEntities: true,

This has fixed the issue.

You are extending BaseEntity this doesn't mean that it will place the @Entity annotation to your entity class. it is basically used like for example certain data fields you want every of your entities to have.

it's internal implementation is something like below :-

// base.entity.ts
import { PrimaryGeneratedColumn, Column, UpdateDateColumn, CreateDateColumn } from 'typeorm';

export abstract class BaseEntity {
@PrimaryGeneratedColumn('uuid')
id: string;

@Column({ type: 'boolean', default: true })
isActive: boolean;

@Column({ type: 'boolean', default: false })
isArchived: boolean;

@CreateDateColumn({ type: 'timestamptz', default: () => 'CURRENT_TIMESTAMP' })
createDateTime: Date;

@Column({ type: 'varchar', length: 300 })
createdBy: string;

@UpdateDateColumn({ type: 'timestamptz', default: () => 'CURRENT_TIMESTAMP' })
lastChangedDateTime: Date;

@Column({ type: 'varchar', length: 300 })
lastChangedBy: string;

@Column({ type: 'varchar', length: 300, nullable: true })
   internalComment: string | null;
}

so this will generate an uuid id-field and/or a createDateTime-, lastChangedDateTime-fields.

Note: that these base classes should be abstract.

so you have to place @Entity annotation at the starting of your each Entity class

also change the order of imports like first import the TypeOrmModule and then TaskModule.

Hope it will help.

that error is caused by typescript if you set typeOrmCOnfig just like this enter image description here

use file after compile, that is to say , use dist/*.js

According to typeOrm official doc loading-all-entities-from-the-directory be careful with this approach while working with ts-node. The outDir might be pointing to a different location/file from where your entities are placed, resulting in the above-mentioned error. You'll need to specify paths to .js files inside outDir directory.

Set entities to :

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

    @Module({
  imports: [
    TasksModule,
    TypeOrmModule.forRoot({
      type: 'postgres',
      host: 'localhost',
      port: 5432,
      username: 'postgres',
      password: 'postgres',
      database: 'userdata',
      autoLoadEntities: true,
      synchronize: true,

    }),
  ],
  
})
export class AppModule { }

Set your entities like this:

 entities: ['dist/src/**/*.entity{.ts,.js}'],
Related