Windows 11下NestJS项目启动失败:找不到模块‘D:\R’
NestJS项目Windows 11启动失败排查方案(Ubuntu可正常运行场景)
检查.env文件编码与加载配置
确保.env文件编码为UTF-8无BOM格式,Windows记事本默认保存的UTF-8带BOM会导致解析异常;同时确认@nestjs/config在app.module.ts中的配置正确,使用path.resolve处理env文件路径,避免Windows路径分隔符问题:import { Module } from '@nestjs/common'; import { ConfigModule } from '@nestjs/config'; import * as path from 'path'; @Module({ imports: [ ConfigModule.forRoot({ isGlobal: true, envFilePath: path.resolve(__dirname, '../.env'), }), ], }) export class AppModule {}处理跨平台依赖兼容性
部分依赖包需要针对平台编译,Ubuntu下的编译产物无法在Windows运行:- 删除
yarn.lock和node_modules后,执行yarn install --force强制重新安装适配Windows的依赖 - 若项目包含
node-gyp编译类依赖(如bcrypt、sharp),需先安装Windows编译环境:安装Visual Studio Build Tools(勾选「使用C++的桌面开发」组件),并配置Python 3.x到系统PATH
- 删除
修正终端与路径问题
- 使用PowerShell或Git Bash执行命令,避免CMD的路径解析限制
- 确保项目根路径不含中文、空格或特殊字符,这类路径会导致文件加载异常
对齐Node.js与Yarn版本
对比Ubuntu与Windows下的node -v、yarn -v输出,用nvm-windows将Windows的Node.js版本切换至与Ubuntu一致,版本差异会引发依赖安装或运行问题定位具体错误栈
执行yarn start:debug启动调试模式,查看完整错误日志,精准定位是模块加载、配置解析还是依赖调用环节出现问题
内容的提问来源于stack exchange,提问作者pndtr_801
相关产品推荐
相关产品推荐

