使用ts-node运行TypeORM迁移时遇deepFlatten及模块查找错误
TypeORM迁移执行时的deepFlatten错误与路径别名问题解决
环境信息
- Node.js版本:v14.5.1
- TypeORM版本:0.3.17
- ts-node版本:10.9.1
- 项目结构:TypeScript混合JS配置文件
问题描述
执行Yarn迁移脚本时,出现以下错误:
Error during migration run: Error: Unable to open file: "/Users/nikola/poddster-maintenence-saas/poddster/backend/src/config/_database.js". (0 , utils_1.deepFlatten) is not a function at Function.loadDataSource (/Users/nikola/poddster-maintenence-saas/poddster/backend/node_modules/src/commands/CommandUtils.ts:22:19) at async Object.handler (/Users/nikola/poddster-maintenence-saas/poddster/backend/node_modules/src/commands/MigrationRunCommand.ts:42:26)
数据库配置文件(简化版)
import { DataSource } from "typeorm"; import { BaseEntity } from "../entity/BaseEntity"; // Additional imports... const dataSourceCache = new Map(); export const initializeDatabase = async dbName => { // Initialization logic... }; // Database configuration function getDatabaseConfig(dbName) { return { type: "postgres", // Other configuration... entities: [ BaseEntity, // Other entities... ], }; } const defaultDbName = process.env.DB_NAME || "default"; export const defaultDataSource = new DataSource(getDatabaseConfig(defaultDbName));
Yarn脚本配置
package.json中的迁移脚本:
"scripts": { "typeorm": "ts-node -r tsconfig-paths/register ./node_modules/.bin/typeorm" }
执行命令:
yarn typeorm migration:run -- -d ./src/config/_database.js
已尝试的解决措施
- 验证ts-node已安装且可访问
- 确认tsconfig.json路径别名配置匹配项目结构
- 简化
_database.js文件以隔离问题,未成功
后续尝试与新错误
修改脚本为:
"scripts": { "typeorm": "node --require ts-node/register ./node_modules/typeorm/cli.js" }
或使用typeorm-ts-node-commonjs后,出现路径别名解析错误:
Error during migration run: Error: Unable to open file: "/Users/nikola/poddster-maintenence-saas/poddster/backend/src/config/_database.js". Cannot find module '~/environment' Require stack: - /Users/nikola/poddster-maintenence-saas/poddster/backend/src/utils/_throw-error.js - /Users/nikola/poddster-maintenence-saas/poddster/backend/src/utils/index.js - /Users/nikola/poddster-maintenence-saas/poddster/backend/src/entity/Booking.ts - /Users/nikola/poddster-maintenence-saas/poddster/backend/src/config/_database.js - /Users/nikola/poddster-maintenence-saas/poddster/backend/node_modules/typeorm/util/ImportUtils.js - /Users/nikola/poddster-maintenence-saas/poddster/backend/node_modules/typeorm/commands/CommandUtils.js - /Users/nikola/poddster-maintenence-saas/poddster/backend/node_modules/typeorm/commands/SchemaSyncCommand.js - /Users/nikola/poddster-maintenence-saas/poddster/backend/node_modules/typeorm/cli.js
但tsconfig已配置路径别名:
"paths": { "~/*": ["src/*"],
解决方案
1. 升级Node.js版本
TypeORM 0.3.17要求Node.js最低版本为v14.17.0,当前v14.5.1低于要求,版本不兼容会导致内部函数(如deepFlatten)无法正常执行。建议升级到v16 LTS或更高版本。
2. 统一配置文件模块格式
将_database.js重命名为_database.ts,避免TS与JS混合导致的模块解析混乱。同时检查自定义工具函数的导出:
- 确保
deepFlatten在utils/index.js中正确导出:export const deepFlatten = (arr) => { // 函数实现逻辑 }; - 若使用默认导出,导入时需对应调整:
import deepFlatten from './utils';
3. 修正Yarn脚本配置
使用正确的ts-node调用方式,确保tsconfig-paths生效:
"scripts": { "typeorm": "ts-node -r tsconfig-paths/register ./node_modules/typeorm/cli.js" }
执行命令时使用TS配置文件:
yarn typeorm migration:run -d ./src/config/_database.ts
4. 完善tsconfig.json配置
确保compilerOptions包含以下关键配置,覆盖JS文件解析与路径别名映射:
{ "compilerOptions": { "module": "CommonJS", "target": "ES6", "esModuleInterop": true, "allowJs": true, "baseUrl": "./", "paths": { "~/*": ["src/*"] }, "outDir": "./dist", "strict": true }, "include": ["src/**/*"], "exclude": ["node_modules"] }
问题原因解析
- deepFlatten错误:Node.js版本过低导致TypeORM内部函数运行异常,或自定义工具函数的导出/导入方式不匹配;混合JS与TS配置文件也会引发模块解析混乱。
- 路径别名问题:直接调用TypeORM的bin脚本不会加载
tsconfig-paths,需通过ts-node -r参数注册才能生效;同时JS文件的路径别名解析需要开启allowJs配置。
内容的提问来源于stack exchange,提问作者user3362334
相关产品推荐
相关产品推荐

