Windows下NestJS启动报错:Cannot find module '../app.module'
NestJS项目Windows启动报错:Cannot find module '../app.module'
执行npm start启动项目时出现如下错误:
Error: Cannot find module '../app.module' Require stack: - E:\Egor\egor-internship-backend\dist\main.js at Function.Module._resolveFilename (node:internal/modules/cjs/loader:1077:15) at Function.Module._load (node:internal/modules/cjs/loader:922:27) at Module.require (node:internal/modules/cjs/loader:1143:19) at require (node:internal/modules/cjs/helpers:121:18) at Object.<anonymous> (E:\Egor\egor-internship-backend\dist\main.js:7:20) at Module._compile (node:internal/modules/cjs/loader:1256:14) at Object.Module._extensions..js (node:internal/modules/cjs/loader:1310:10) at Module.load (node:internal/modules/cjs/loader:1119:32) at Function.Module._load (node:internal/modules/cjs/loader:960:12) at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
当前环境:NestJS版本10.1.16,Node.js版本v18.17.1。
问题核心:编译生成的dist/main.js中,引用app.module的路径错误地使用了../而非./,代码如下:
const _appmodule = require("../app.module");
该问题仅在Windows系统出现,Linux和MacOS下运行正常。已尝试以下操作但均无效:
- 重装Node.js、Nest CLI、WebStorm
- 切换dev/debug/prod模式
- 删除dist和node_modules文件夹并重新安装依赖
项目配置文件
tsconfig.json
{ "compilerOptions": { "moduleResolution": "node", "target": "es2022", "noEmit": true, "declaration": true, "removeComments": true, "emitDecoratorMetadata": true, "experimentalDecorators": true, "allowSyntheticDefaultImports": true, "baseUrl": "./src", "outDir": "./dist", "incremental": true, "skipLibCheck": true, "strictNullChecks": false, "noImplicitAny": false, "noImplicitOverride": false, "noImplicitReturns": true, "strictBindCallApply": false, "forceConsistentCasingInFileNames": true, "noUnusedLocals": true, "noUnusedParameters": true, "strict": true, "allowJs": false, "allowUnreachableCode": false, "allowUnusedLabels": false, "esModuleInterop": true, "tsBuildInfoFile": "./dist/tsbuildinfo.json", } }
tsconfig.build.json
{ "extends": "./tsconfig.json", "exclude": ["node_modules", "test", "dist", "**/*spec.ts"], "compilerOptions": { "noEmit": false, "sourceMap": true, "incremental": true, "outDir": "./dist" } }
nest-cli.json
{ "$schema": "https://json.schemastore.org/nest-cli", "collection": "@nestjs/schematics", "sourceRoot": "src", "compilerOptions": { "builder": "swc", "typeCheck": true } }
项目目录结构
项目根目录下包含src、dist、node_modules等文件夹,src文件夹内有main.ts、app.module.ts及其他业务模块文件,编译产物输出到根目录的dist文件夹中。
解决方案建议
1. 切换回TypeScript默认编译器(替换SWC)
当前使用SWC作为构建器,可能存在Windows路径解析兼容性问题。修改nest-cli.json的compilerOptions,移除SWC配置:
{ "$schema": "https://json.schemastore.org/nest-cli", "collection": "@nestjs/schematics", "sourceRoot": "src", "compilerOptions": { "typeCheck": true } }
删除dist文件夹后,重新执行npm run build和npm start。
2. 调整tsconfig路径映射
在tsconfig.json的compilerOptions中添加paths配置,明确路径映射规则:
"compilerOptions": { // 原有配置... "paths": { "@/*": ["*"] } }
同时将main.ts中的导入改为import { AppModule } from '@/app.module';,让TypeScript正确解析路径。
3. 检查main.ts导入路径
确认main.ts中使用的相对路径是否正确,比如import { AppModule } from './app.module';,确保编译后路径能被正确转换。
4. 升级NestJS及相关依赖
尝试升级到NestJS最新稳定版本,可能该问题已被修复:
npm install @nestjs/core@latest @nestjs/common@latest @nestjs/cli@latest
内容的提问来源于stack exchange,提问作者Egorii
相关产品推荐
相关产品推荐

