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

导入本地模块失败:Error: Cannot find module '../Definitions/Types'

TypeScript模块找不到问题:仅特定文件导入types.d.ts报错

运行TypeScript项目测试时,仅两个文件导入types.d.ts时出现「模块找不到」错误,其余文件导入均正常。项目文件结构为:根目录Command Handler下包含Definitions文件夹(内有types.d.ts)、Handlers文件夹(内有handlers.ts),以及根目录的index.ts。

导入语句

  • 根目录index.ts的导入代码:
import { CommandJSONExport, CommandInterface } from "./Definitions/types";
  • Handlers/handlers.ts的导入代码:
import { CommandInterface, CommandJSONExport, ConstructorOptions } from "../Definitions/types";

报错信息

Error: Cannot find module '../Definitions/types'
Require stack:
- F:\Coding\Command Handler\Handlers\handlers.ts
- F:\Coding\Command Handler\index.ts
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
    at Function.Module._resolveFilename.sharedData.moduleResolveFilenameHook.installedValue [as _resolveFilename] (C:\Users\sebas\AppData\Roaming\npm\node_modules\ts-node\node_modules\@cspotcode\source-map-support\source-map-support.js:679:30)
    at Function.Module._load (node:internal/modules/cjs/loader:778:27)
    at Module.require (node:internal/modules/cjs/loader:1005:19)
    at require (node:internal/modules/cjs/helpers:102:18)
    at Object.<anonymous> (F:\Coding\Command Handler\Handlers\handlers.ts:3:1)
    at Module._compile (node:internal/modules/cjs/loader:1105:14)
    at Module.m._compile (C:\Users\sebas\AppData\Roaming\npm\node_modules\ts-node\src\index.ts:1371:23)
    at Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
    at Object.require.extensions.<computed> [as .ts] (C:\Users\sebas\AppData\Roaming\npm\node_modules\ts-node\src\index.ts:1374:12) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [
    'F:\\Coding\\Command Handler\\Handlers\\handlers.ts',
    'F:\\Coding\\Command Handler\\index.ts'
  ]
}

补充说明

  • 当前工作目录为Command Handler文件夹
  • 移除handlers.ts中的types.d.ts导入可消除错误,但会引发其他类型相关问题,必须保留该导入

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.19 17:15:36