NestJS SyntaxError:不能在模块外使用导入语句

时间:2021-07-21 06:46:24

标签: node.js typescript nestjs typeorm

我在迁移此迁移时出错:

import { MigrationInterface, QueryRunner } from "typeorm";


export class addIdColumnToBidProductsProductTable1626803008084 implements MigrationInterface {

public async up(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.query('ALTER TABLE `bid_products_product` ADD id INT PRIMARY KEY AUTO_INCREMENT');
}

public async down(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.query('ALTER TABLE `bid_products_product` DROP COLUMN `id`');
}

}

这是我的错误:

> bid@0.0.1 migration:show
> typeorm migration:show

Error during migration show:
/home/antikon/Projects/bid/src/migrations/1626803008084-addIdColumnToBidProductsProductTable.ts:1
import { MigrationInterface, QueryRunner } from "typeorm";
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at Object.compileFunction (node:vm:352:18)
    at wrapSafe (node:internal/modules/cjs/loader:1025:15)
    at Module._compile (node:internal/modules/cjs/loader:1059:27)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1124:10)
    at Module.load (node:internal/modules/cjs/loader:975:32)
    at Function.Module._load (node:internal/modules/cjs/loader:816:12)
    at Module.require (node:internal/modules/cjs/loader:999:19)
    at require (node:internal/modules/cjs/helpers:93:18)
    at /home/antikon/Projects/bid/node_modules/typeorm/util/DirectoryExportedClassesLoader.js:42:39
    at Array.map (<anonymous>)

这是我的 typeorm 连接:

TypeOrmModule.forRoot({
      type: 'mysql',
      host: 'localhost',
      port: 3306,
      username: 'root',
      password: 'grish2003',
      database: 'biddb',
      entities: [User, Bid, Product, Bid_Product, Bet],
      synchronize: true,
      migrations: [
        join(__dirname, 'migrations/*{.ts}')
      ]
    })

在 tsconfig.json 文件中:

{
  "compilerOptions": {
    "module": "commonjs",
    "declaration": true,
    "removeComments": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "allowSyntheticDefaultImports": true,
    "target": "es2017",
    "sourceMap": true,
    "outDir": "./dist",
    "baseUrl": "./",
    "incremental": true
  }
}

我想通过迁移在数据库中添加一列,但是出现了这个错误。我尝试了很多东西,但没有任何结果。 请告诉我为什么会出现此错误。谢谢

0 个答案:

没有答案