【问题标题】:syntax error typeorm when trying to run the migration尝试运行迁移时出现语法错误 typeorm
【发布时间】:2021-07-29 08:06:29
【问题描述】:

当我运行 npx typeorm migration:run 时出现这样的错误:

语法错误:不能在模块外使用 import 语句。

我问了很多人,我不知道如何解决。 这是我的 ormconfig 和我的 package.json 分别:

{
  "type": "postgres",
  "host": "localhost",
  "port": 5432,
  "username": "docker",
  "password": "docker",
  "database": "db_project_manager",
  "entities": [
    "./src/models/**/*.ts"
  ],
  "migrations": [
    "./src/database/migrations/**/*.ts"
  ],
  "cli": {
    "migrationsDir": "./src/database/migrations"
  }
}




{
  "name": "backend",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "dev": "ts-node-dev --inspect --transpile-only --ignore-watch node_modules src/server.ts",
    "typeorm": "ts-node-dev ./node_modules/typeorm/cli.js"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "express": "^4.17.1",
    "jsonwebtoken": "^8.5.1",
    "pg": "^8.6.0",
    "reflect-metadata": "^0.1.13",
    "typeorm": "0.2.24"
  },
  "devDependencies": {
    "@types/express": "^4.17.11",
    "eslint": "^6.8.0",
    "ts-node-dev": "^1.1.6",
    "typescript": "^4.2.4",
    "@typescript-eslint/eslint-plugin": "^3.4.0",
    "@typescript-eslint/parser": "^3.4.0",
    "eslint-config-airbnb-base": "^14.2.0",
    "eslint-config-prettier": "^6.11.0",
    "eslint-import-resolver-typescript": "^2.0.0",
    "eslint-plugin-import": "^2.21.2",`enter code here`
    "eslint-plugin-prettier": "^3.1.4"enter code here
  }
}

【问题讨论】:

    标签: typescript typeorm


    【解决方案1】:

    我认为 typeorm 迁移不支持开箱即用的 Typescript 文件。您可能需要更改您的package.json,如下所示:

    "entities": [
      "<PATH_TO_YOUR_DIST>/models/**/*{.js,.ts}"
    ],
    "migrations": [
      "<PATH_TO_YOUR_DIST>/database/migrations/**/*{.js,.ts}"
    ],
    

    您可以在tsconfig.json 文件中找到&lt;PATH_TO_YOUR_DIST&gt; -> compilerOptions -> outDir

    有时您的&lt;PATH_TO_YOUR_DIST&gt; 中可能有一个src 目录。在这种情况下,您还必须将其附加到路径 (&lt;PATH_TO_YOUR_DIST&gt;/src) 中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-11-28
      • 2022-01-12
      • 1970-01-01
      • 1970-01-01
      • 2015-07-10
      • 2021-03-28
      • 2020-10-30
      • 1970-01-01
      相关资源
      最近更新 更多