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

迁移Strapi项目至TypeScript时遇'in'操作符错误,求解决方向

Strapi 4.15.5 迁移TypeScript后启动报错:Cannot use 'in' operator to search for 'Symbol(StrapiCustomCoreController)' in undefined

问题场景

将Strapi项目迁移至TypeScript,升级所有Strapi包至4.15.5版本并将文件重命名为.ts后缀后,执行strapi develop命令时出现以下错误:

[2024-01-03 10:50:48.102] debug: ⛔️ Server wasn't able to start properly.
[2024-01-03 10:50:48.103] error: Cannot use 'in' operator to search for 'Symbol(StrapiCustomCoreController)' in undefined
TypeError: Cannot use 'in' operator to search for 'Symbol(StrapiCustomCoreController)' in undefined
    at isCustomController (/home/dev/gn/strapi-cms/node_modules/@strapi/strapi/dist/factories.js:80:35)
    at Array.filter (<anonymous>)
    at Strapi.sendStartupTelemetry (/home/dev/gn/strapi-cms/node_modules/@strapi/strapi/dist/Strapi.js:243:68)
    at Strapi.postListen (/home/dev/gn/strapi-cms/node_modules/@strapi/strapi/dist/Strapi.js:266:10)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async Server.onListen (/home/dev/gn/strapi-cms/node_modules/@strapi/strapi/dist/Strapi.js:276:11)

错误根源是Strapi核心代码中isCustomController方法接收的controller参数为undefined。

解决方向

1. 检查自定义Controller的导出格式

迁移TS后,部分Controller可能存在导出错误:

  • 确保每个controller文件都正确导出Strapi控制器对象,比如使用createCoreController的标准格式:
    import { createCoreController } from '@strapi/strapi';
    
    export default createCoreController('api::post.post', ({ strapi }) => ({
      // 自定义方法实现
    }));
    
  • 排查所有src/api/**/controllers目录下的文件,避免空导出、错误导出语法(比如仅导出类型而未导出控制器实例)导致的undefined。

2. 校验路由与Controller的关联

  • 检查src/api/**/routes.ts中的路由配置,确保controller字段指向的控制器名称与实际文件一致,无拼写错误或路径错误。
  • 确认没有路由配置指向已删除或未迁移的控制器文件。

3. 清理缓存并重新构建

执行以下命令清理残留缓存与构建文件,再重新安装依赖启动:

rm -rf node_modules .cache build
npm install
strapi develop

Strapi的缓存可能残留旧JS文件,导致加载逻辑异常。

4. 检查TypeScript配置文件

确保项目根目录的tsconfig.json符合Strapi TS项目要求,关键配置示例:

{
  "compilerOptions": {
    "target": "ES2020",
    "module": "commonjs",
    "lib": ["ES2020"],
    "rootDir": "./",
    "outDir": "./build",
    "strict": true,
    "esModuleInterop": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true,
    "resolveJsonModule": true,
    "moduleResolution": "node",
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true
  },
  "include": ["./**/*"],
  "exclude": ["node_modules", "build"]
}

同时确认所有Strapi相关依赖包(如@strapi/strapi、@strapi/admin等)版本统一为4.15.5。

5. 排查第三方插件与自定义代码

  • 如果使用了自定义插件,检查插件内的控制器是否也完成TS迁移,导出格式正确。
  • 排查是否存在修改Strapi核心控制器加载逻辑的自定义代码,导致控制器实例未正确生成。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.02 21:06:31