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

配置tsconfig paths后深层文件无法导入core模块的解决方法

解决深层文件无法导入@core模块的问题

1. 修正tsconfig的include配置

你的include字段仅包含了core/lib,但core/schema目录不在其中,TypeScript无法识别该目录下的文件类型声明。修改include,将整个core目录纳入范围:

"include": ["apps/", "index.ts", "core/**/*"],

2. 同步构建工具的别名配置(若使用)

TypeScript的paths配置仅用于类型检查,实际代码构建时(如Webpack、Vite、Rollup),需要单独配置别名映射,否则构建工具会无法找到模块:

  • Webpack:在webpack.config.js中添加resolve.alias
module.exports = {
  resolve: {
    alias: {
      '@core': path.resolve(__dirname, './core')
    }
  }
}
  • Vite:在vite.config.ts中添加resolve.alias
import { defineConfig } from 'vite'
import path from 'path'

export default defineConfig({
  resolve: {
    alias: {
      '@core': path.resolve(__dirname, './core')
    }
  }
})

3. 重启TypeScript服务

修改tsconfig后,编辑器的TypeScript服务可能需要重启才能生效:

  • 按Ctrl+Shift+P(Windows/Linux)或Cmd+Shift+P(Mac)
  • 选择TypeScript: Restart TS Server

完成以上步骤后,深层目录下的file1.tsx即可直接使用import { something } from '@core/schema/auth'导入根目录的core模块。

内容的提问来源于stack exchange,提问作者The Blind Hawk

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.12 12:34:57