尝试运行迁移时出现语法错误 typeorm

时间:2021-05-07 01:10:11

标签: typescript typeorm

当我运行 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
  }
}

1 个答案:

答案 0 :(得分:0)

我认为 typeorm 迁移不支持开箱即用的 Typescript 文件。您可能需要像下面那样更改您的 package.json

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

您可以在 <PATH_TO_YOUR_DIST> 文件中找到 tsconfig.json -> compilerOptions -> outDir

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