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

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

提交回复
热议问题