You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

TypeORM执行migration:run时出现Experimental support装饰器错误

TypeORM执行migration:run时TS1219错误的解决方法

执行migration:run命令时触发以下错误:

error TS1219: Experimental support for decorators is a feature that is subject to change in a future release. Set the 'experimentalDecorators' option in your 'tsconfig' or 'jsconfig' to remove this warning.

已在tsconfig.base.json、tsconfig.json中配置"experimentalDecorators": true,"emitDecoratorMetadata": true,VSCode也开启对应设置,但问题依旧。项目环境:typeorm:0.3.10、node:16.13.1,采用monorepo结构。

可能的解决方法

1. 让ts-node显式读取指定的tsconfig文件

ts-node在monorepo结构下可能无法自动识别tsconfig的继承关系,需要在TypeORM脚本中指定tsconfig路径。修改package.json中的typeorm脚本:

"typeorm": "node --require ts-node/register -r tsconfig-paths/register ./node_modules/typeorm/cli.js -d ./apps/api/ormconfig.ts --tsconfig ./apps/api/tsconfig.json"

若需要使用根目录的基础配置,可改为:

"typeorm": "node --require ts-node/register -r tsconfig-paths/register ./node_modules/typeorm/cli.js -d ./apps/api/ormconfig.ts --tsconfig ./tsconfig.base.json"

2. 检查tsconfig的include/exclude规则

确保tsconfig的include字段包含entity、migration文件所在目录,避免exclude误排除相关文件。例如在tsconfig.base.json中添加:

"include": [
  "apps/api/src/**/*.ts",
  "apps/api/ormconfig.ts"
]

3. 升级ts-node版本

旧版ts-node与TypeORM 0.3.x可能存在兼容性问题,执行升级命令:

npm install ts-node@latest --save-dev

4. 用tsx替代ts-node(可选)

若ts-node问题无法解决,可尝试用tsx运行TypeORM CLI:

  1. 安装tsx:
npm install tsx --save-dev
  1. 修改package.json脚本:
"typeorm": "tsx ./node_modules/typeorm/cli.js -d ./apps/api/ormconfig.ts"

5. 修正ormconfig.ts中的路径配置

确保entities和migrations的路径准确,可改用绝对路径避免相对路径歧义:

import { join } from 'path';
import { DataSource, DataSourceOptions } from "typeorm";

const ormconfig: DataSourceOptions = {
    migrationsTableName: 'migrations',
    type: 'postgres',
    url: "SOMEURL",
    synchronize: false,
    logging: true,
    migrationsRun: true,
    entities: [join(__dirname, 'src/models/**/*.entity.ts')],
    migrations: [join(__dirname, 'src/db/migrations/*.ts')],
};

const ormDatasource = new DataSource(ormconfig);
export default ormDatasource;

6. 确认未混用旧版TypeORM配置

TypeORM 0.3.x已废弃旧版ormconfig格式,确保项目中不存在ormconfig.json等旧配置文件,完全使用DataSource模式。

内容的提问来源于stack exchange,提问作者iBUYPOWER

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.13 11:10:34