TypeORM Entity in NESTJS - Cannot use import statement outside a module

后端 未结 10 1811
甜味超标
甜味超标 2020-12-03 10:01

Started new project with \'nest new\' command. Works fine until I add entity file to it.

Got following error:

import { Entity, Column, Primary

10条回答
  •  爱一瞬间的悲伤
    2020-12-03 10:46

    My assumption is that you have a TypeormModule configuration with an entities property that looks like this:

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

    or like

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

    The 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

    entities: [join(__dirname, '**', '*.entity.{ts,js}`)]
    

    where 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.

    EDIT 1/10/2020

    The 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

    entities: ["dist/**/*.entity.js"]
    

    so that you are using the compiled js files and have no chance to use the ts files in your code.

提交回复
热议问题