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

后端 未结 10 1800
甜味超标
甜味超标 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:29

    You need to have a something.module.ts for every section of your app. It works like Angular. This is setup with GraphQL resolvers and service. REST is a bit different with a controller. Each module will probably have an entity and if GraphQL, projects.schema.graphql.

    projects.module.ts

    import { Module } from '@nestjs/common';
    import { TypeOrmModule } from '@nestjs/typeorm';
    import { ProjectsService } from './projects.service';
    import { Projects } from './projects.entity';
    
    import { ProjectsResolvers } from './projects.resolvers';
    
    @Module({
      imports: [
        TypeOrmModule.forFeature([Projects])],
      providers: [
        ProjectsService,
        ProjectsResolvers
      ],
    
    })
    
    export class ProjectsModule {}
    
    0 讨论(0)
  • 2020-12-03 10:30

    As Jay McDoniel explained in his answer, the problem seems to be the pattern matching of entity files in ormconfig.json file: Probably a typescript file (module) is imported from a javascript file (presumably a previously transpiled typescript file).

    It should be sufficient to remove an existing ts glob pattern in the ormconfig.json, so that TypeORM will only load javascript files. The path to the entity files should be relative to the working directory where node is executed.

       "entities"   : [
          "dist/entity/**/*.js"
       ],
       "migrations" : [
          "dist/migration/**/*.js"
       ],
       "subscribers": [
          "dist/subscriber/**/*.js"
       ],
    
    0 讨论(0)
  • 2020-12-03 10:31

    Actually, typeorm was designed to work with javascript by default.

    To run the migrations with typescript, you must tell typeorm to do it.

    Just put in your package.json, in the scripts part this line below:

    "typeorm": "ts-node-dev ./node_modules/typeorm/cli.js"
    

    and then, try to migrate again:

    yarn typeorm migration:run
    
    0 讨论(0)
  • This worked for me - no changes needed to your ormconfig.js. Run from your root directory where the node_modules are:

    ts-node ./node_modules/typeorm/cli.js  migration:generate -n <MirgrationName> -c <ConnectionType>
    

    Example:

    ts-node ./node_modules/typeorm/cli.js  migration:create -n AuthorHasMultipleBooks -c development 
    
    0 讨论(0)
  • 2020-12-03 10:37

    Defining the entities property in ormconfig.json as mentioned in the official documentation resolved this issue for me.

    // This is your ormconfig.json file
    
    ...
    "entities": ["dist/**/*.entity{.ts,.js}"]
    ...
    
    0 讨论(0)
  • 2020-12-03 10:38

    In the TypeORM documentation, i found a specific section for Typescript.

    This section says:

    Install ts-node globally:

    npm install -g ts-node
    

    Add typeorm command under scripts section in package.json

    "scripts" {
        ...
        "typeorm": "ts-node -r tsconfig-paths/register ./node_modules/typeorm/cli.js"    
    }
    

    Then you may run the command like this:

    npm run typeorm migration:run
    

    If you need to pass parameter with dash to npm script, you will need to add them after --. For example, if you need to generate, the command is like this:

    npm run typeorm migration:generate -- -n migrationNameHere
    

    This works with my file config:

    {
        "type": "postgres",
        "host": "yourhost",
        "port": 5423,
        "username": "username",
        "password": "password",
        "database": "your_db",
        "synchronize": true,
        "entities": [
            "src/modules/**/*.entity.{ts,js}"
        ],
        "migrations": [
            "src/migrations/**/*.{ts,js}"
        ],
        "cli": {
            "entitiesDir": "src/modules",
            "migrationsDir": "src/migrations"
        }
    }
    

    Then you can run the generate command.

    0 讨论(0)
提交回复
热议问题