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

TypeScript项目执行npm start报Cannot find module错误求助

解决"Cannot find module '.../index.js'"错误的方案

1. 修正package.json的入口指向

你的package.json里main字段配置的是根目录的index.js,但这个文件并不存在,实际编译后的入口是dist/server.js,修改如下:

"main": "dist/server.js"

2. 排查代码中的错误导入路径

错误提示要找根目录的index.js,基本是因为src/server.js里用了错误的相对路径导入。比如如果你的代码里写了:

import config from '../index.js';

但实际要导入的是src目录下的index.ts,那正确的路径应该是:

import config from './index.ts';

编译后dist/server.js会自动对应到dist/index.js,不会再去根目录找文件。

3. 调整模块编译配置(解决ES模块兼容问题)

当前tsconfig.json里module设为ES6,同时package.json开启了"type": "module",Node.js处理ES模块时要求严格的文件后缀,容易出现解析问题。可以改成CommonJS适配Node.js默认机制:
在tsconfig.json中修改:

"compilerOptions": {
  // 保留其他配置
  "module": "CommonJS"
}

同时移除package.json中的"type": "module"字段,避免混合模块系统引发冲突。

4. 确认编译产物完整性

检查dist目录,确保所有src下的文件都被正确编译到dist里,包括server.js和依赖的其他文件,避免编译遗漏导致的模块缺失。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.23 08:15:36