nestjs - 语法错误 : Unexpected token { import {MigrationInterface, QueryRunner} 来自 "typeorm"

标签 nestjs typeorm

我最近正在使用 NestJs 和 typeORM,但在运行 Nest 应用程序时遇到错误。它会生成此错误

/home/backend/src/migrations/1611061187746-loadAllEntities.ts:1
import { MigrationInterface, QueryRunner } from 'typeorm';
       ^

SyntaxError: Unexpected token {
    at Module._compile (internal/modules/cjs/loader.js:723:23)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)

该错误指向我生成的迁移 ts 文件

//xxxxx-migration.ts

import {MigrationInterface, QueryRunner} from "typeorm";

export class migrationMessageDetail1611143681385 implements MigrationInterface {
    name = 'migrationMessageDetail1611143681385'

    public async up(queryRunner: QueryRunner): Promise<void> {
        await queryRunner.query("CREATE TABLE `detail_pricing` (`id` int NOT NULL AUTO_INCREMENT, `dayStart` int NOT NULL, `dayEnd` int NOT NULL, `isCommonChoice` tinyint NOT NULL, `price` int NOT NULL DEFAULT '200', `pricePerday` int NOT NULL, `discount` int NOT NULL, `currency` varchar(255) NOT NULL, `withDeposit` int NOT NULL DEFAULT '30', `noDeposit` varchar(255) NOT NULL DEFAULT '56', `carId` int NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB");
        
....

这是 ormconfig.ts 文件

//ormconfig.ts
import { ConnectionOptions } from 'typeorm';

const connectionOptions: ConnectionOptions = {
  type: 'mysql',
  host: 'localhost',
  port: 3306,
  username: 'root',
  password: '',
  database: 'cave',
  synchronize: false, 
  migrationsRun: true,
  logging: ['warn', 'error'],
  entities: ['dist/src/domain/persistence/entities/**/*.js'],
  migrationsTableName: 'migrations',
  migrations: ['src/migrations/*.ts'], 
  subscribers: ['src/subscriber/**/*.ts'],
  cli: {
    entitiesDir: 'src/domain/persistence/entities',
    migrationsDir: 'src/migrations',
    subscribersDir: 'src/subscriber',
  },
};

export default connectionOptions;

这是我的配置 tsconfig 文件

//tsconfig.json
{
   "compilerOptions": {
      "lib": [
         "es5",
         "es6",
         "es2020",
         "esnext"
      ],
      "resolveJsonModule": true,
      "module": "commonjs",
      "declaration": true,
      "removeComments": true,
      "emitDecoratorMetadata": true,
      "experimentalDecorators": true,
      "allowSyntheticDefaultImports": true,
      "target": "es2020",
      "sourceMap": true,
      "outDir": "./dist",
      "baseUrl": "./",
      "esModuleInterop": true
   }
}

我已经尝试过了

  • migrations: ['src/migrations/*.ts'], 更改为 [__dirname + 'migrations/*{.ts/.js}'],在 ormconfig.ts 中,运行应用程序时没有错误,但migration:run 不起作用..

这是我如何运行和生成迁移

#generate migration
$ ts-node -r tsconfig-paths/register ./node_modules/typeorm/cli.js --config src/configs/ormconfig.ts migration:generate -n migrationMessageDetail -d src/migrations

#run migration
$ ./node_modules/.bin/ts-node ./node_modules/typeorm/cli.js --config src/configs/ormconfig.ts migration:run

这是我的包 json

{
  "name": "cave-backend",
  "version": "0.0.1",
  "description": "",
  "author": "",
  "private": true,
  "license": "UNLICENSED",
  "scripts": {
    "prebuild": "rimraf dist",
    "build": "nest build",
    "format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
    "start": "nest start",
    "start:dev": "nest start --watch",
    "start:debug": "nest start --debug --watch",
    "start:prod": "node dist/main",
    "lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
    "test": "jest",
    "test:watch": "jest --watch",
    "test:cov": "jest --coverage",
    "test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
    "test:e2e": "jest --config ./test/jest-e2e.json"
  },
  "dependencies": {
    "@golevelup/nestjs-rabbitmq": "^1.15.2",
    "@nestjs/common": "^7.5.5",
    "@nestjs/config": "^0.6.1",
    "@nestjs/core": "^7.5.5",
    "@nestjs/mapped-types": "^0.1.1",
    "@nestjs/passport": "^7.1.5",
    "@nestjs/platform-express": "^7.5.5",
    "@nestjs/platform-fastify": "^7.5.5",
    "@nestjs/swagger": "^4.7.5",
    "@nestjs/typeorm": "^7.1.5",
    "@turf/distance": "^6.0.1",
    "bcrypt": "^5.0.0",
    "class-transformer": "^0.3.1",
    "class-validator": "^0.12.2",
    "dotenv": "^8.2.0",
    "fastify-helmet": "^5.0.3",
    "fastify-swagger": "^3.5.0",
    "jwks-rsa": "^1.12.0",
    "md5": "^2.3.0",
    "mysql": "^2.18.1",
    "nexmo": "^2.9.1",
    "passport": "^0.4.1",
    "passport-jwt": "^4.0.0",
    "reflect-metadata": "^0.1.13",
    "rimraf": "^3.0.2",
    "rxjs": "^6.6.3",
    "swagger-ui-express": "^4.1.5",
    "typeorm": "^0.2.29",
    "uuid": "^8.3.1"
  },
  "devDependencies": {
    "@nestjs/cli": "^7.5.3",
    "@nestjs/schematics": "^7.2.2",
    "@nestjs/testing": "^7.5.5",
    "@types/cache-manager": "^2.10.3",
    "@types/express": "^4.17.9",
    "@types/jest": "^26.0.15",
    "@types/node": "^14.14.10",
    "@types/supertest": "^2.0.10",
    "@typescript-eslint/eslint-plugin": "^4.8.2",
    "@typescript-eslint/parser": "^4.8.2",
    "eslint": "^7.14.0",
    "eslint-config-prettier": "^6.15.0",
    "eslint-plugin-prettier": "^3.1.4",
    "jest": "^26.6.3",
    "prettier": "^2.2.1",
    "supertest": "^6.0.1",
    "ts-jest": "^26.4.4",
    "ts-loader": "^8.0.11",
    "ts-node": "^9.0.0",
    "tsconfig-paths": "^3.9.0",
    "typescript": "^4.1.2"
  },
  "jest": {
    "moduleFileExtensions": [
      "js",
      "json",
      "ts"
    ],
    "rootDir": "src",
    "testRegex": ".*\\.spec\\.ts$",
    "transform": {
      "^.+\\.(t|j)s$": "ts-jest"
    },
    "collectCoverageFrom": [
      "**/*.(t|j)s"
    ],
    "coverageDirectory": "../coverage",
    "testEnvironment": "node"
  }
}

我可以生成并运行迁移,但我不知道为什么会发生此错误 SyntaxError: Unexpected token { import {MigrationInterface, QueryRunner} from "typeorm" 请帮忙,非常感谢

最佳答案

您是否尝试过更改此设置:
迁移:['src/migrations/*.ts'],
对此:
迁移:['src/migrations/*{.ts,.js}'],

关于nestjs - 语法错误 : Unexpected token { import {MigrationInterface, QueryRunner} 来自 "typeorm",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65809402/

相关文章:

javascript - 如何在typeORM中的@ManyToMany中保存关系

javascript - 恢复 TypeORM 中的特定迁移

postgresql - TypeORM 条件可为空?

postgresql - 如果我从 typeorm 迁移表中删除一条记录会怎样?

node.js - NestJS - 无法将服务注入(inject)订阅者

node.js - 使用 typegoose 时出现 GraphQLError : Type Query must define one or more fields.

typescript - 如何在 Nest 中使用内存数据库和 TypeORM

node.js - Mongoose/MongoDb 在数组中查找具有反向引用的文档

javascript - 在Nestjs中将文件保存在文件系统中

mysql - 无法使用 leftjoin 从 2 个表中获取所有数据