SyntaxError: Unexpected token import MigrationInterface, QueryRunner from "typeorm"

Posted

技术标签:

【中文标题】SyntaxError: Unexpected token import MigrationInterface, QueryRunner from "typeorm"【英文标题】:SyntaxError: Unexpected token import MigrationInterface, QueryRunner from "typeorm"SyntaxError: Unexpected token import MigrationInterface, QueryRunner from "typeorm" 【发布时间】:2021-04-24 19:25:58 【问题描述】:

我最近在使用 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
   

我试过了

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

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

#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" 请帮忙,非常感谢

【问题讨论】:

你能分享你的 package.json 吗?我想知道你是如何运行迁移命令的。 @uraway 我编辑了我的问题.. 【参考方案1】:

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

【讨论】:

以上是关于SyntaxError: Unexpected token import MigrationInterface, QueryRunner from "typeorm"的主要内容,如果未能解决你的问题,请参考以下文章

求教php运行时出错:Parse error: syntax error, unexpected T_STRING, expecting T_FUNCTION 。。。

Import Unexpected identifier + SyntaxError: Unexpected string

js动态加载的a标签的onclick事件参数传递,一直报Uncaught SyntaxError: Invalid or unexpected token

解析错误:语法错误,意外的“未设置”(T_UNSET)

SyntaxError: unexpected EOF while parsing

获取 MongoDB 错误 - SyntaxError: Unexpected token?