{"id":498,"hash":"f327920e8d2209b14ec7d864b216613d2465df80390c1e2911955f4258e79603","pattern":"TypeORM Entity in NESTJS - Cannot use import statement outside a module","full_message":"Started new project with 'nest new' command. Works fine until I add entity file to it.\n\nGot following error:\n\n  import { Entity, Column, PrimaryGeneratedColumn } from 'typeorm';\n\n  \n  ^^^^^^\n\n  \n  SyntaxError: Cannot use import statement outside a module\n\nWhat do I miss?\n\nAdding Entity to Module:\n\nimport { Module } from '@nestjs/common';\nimport { BooksController } from './books.controller';\nimport { BooksService } from './books.service';\nimport { BookEntity } from './book.entity';\nimport { TypeOrmModule } from '@nestjs/typeorm';\n\n@Module({\n  imports: [TypeOrmModule.forFeature([BookEntity])],\n  controllers: [BooksController],\n  providers: [BooksService],\n})\nexport class BooksModule {}\n\napp.module.ts:\n\nimport { Module } from '@nestjs/common';\nimport { AppController } from './app.controller';\nimport { AppService } from './app.service';\nimport { TypeOrmModule } from '@nestjs/typeorm';\nimport { Connection } from 'typeorm';\nimport { BooksModule } from './books/books.module';\n\n@Module({\n  imports: [TypeOrmModule.forRoot()],\n  controllers: [AppController],\n  providers: [AppService],\n})\nexport class AppModule {}","ecosystem":"npm","package_name":"nestjs","package_version":null,"solution":"My assumption is that you have a TypeormModule configuration with an entities property that looks like this:\n\nentities: ['src/**/*.entity.{ts,js}']\n\nor like\n\nentities: ['../**/*.entity.{ts,js}']\n\nThe error you are getting is because you are attempting to import a ts file in a js context. So long as you aren't using webpack you can use this instead so that you get the correct files\n\nentities: [join(__dirname, '**', '*.entity.{ts,js}')]\n\nwhere join is imported from the path module. Now __dirname will resolve to src or dist and then find the expected ts or js file respectively. let me know if there is still an issue going on.\n\nEDIT 1/10/2020\nThe above assumes the configuration is done is a javascript compatible file (.js or in the TypeormModule.forRoot() passed parameters). If you are using an ormconfig.json instead, you should use\n\nentities: [\"dist/**/*.entity.js\"]\n\nso that you are using the compiled js files and have no chance to use the ts files in your code.\n\nOr use\n\nautoLoadEntities: true,","confidence":0.95,"source":"stackoverflow","source_url":"https://stackoverflow.com/questions/59435293/typeorm-entity-in-nestjs-cannot-use-import-statement-outside-a-module","votes":150,"created_at":"2026-04-19T04:51:16.266007+00:00","updated_at":"2026-04-19T04:51:16.266007+00:00"}