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

IntelliJ运行WSL环境下NPM脚本报错‘Failed to prepare environment’求助

解决IntelliJ IDEA连接WSL项目时的环境准备错误与文件同步警告

问题描述

使用IntelliJ IDEA Ultimate 2022.2在Windows 11系统上运行WSL(Ubuntu 20.04)中的NestJS项目,已将Node和NPM指向WSL内通过nvm安装的Node 15,但遇到以下问题:

  1. 运行NPM脚本或打开TS文件时触发错误:
    Failed to prepare environment: Key UploadRoot(localRootPath=\\wsl.localhost\Ubuntu home\nabeelparkar dev\beanstalk\buzz-one-backend, targetRootPath=Temporary(hint=null, prefix=null, parent Directory=null), removeAtShutdown=false) is missing in the map.
    
  2. 启动IDE时收到警告:
    External file changes sync may be slow
    IntelliJ IDEA cannot receive filesystem event notifications for the project. Is it on a network drive?
    

项目目录位于WSL中,NPM脚本在终端可正常运行,但IDE内无法执行。

解决方案

1. 修正WSL项目的打开方式

  • 不要通过Windows文件系统路径(如\\wsl.localhost\Ubuntu\...)打开项目,而是在IntelliJ中选择File > Open,通过IDE内置的WSL文件系统入口访问:
    1. 在文件选择窗口左侧找到WSL选项(未显示则点击+添加WSL分发版)
    2. 选择Ubuntu 20.04,导航到项目的Linux路径(如/home/nabeelparkar dev/beanstalk/buzz-one-backend)并打开

2. 配置Node/NPM解释器为WSL版本

  • 打开File > Settings > Languages & Frameworks > Node.js and NPM
  • 点击解释器下拉框,选择Add,再选择WSL
  • 在弹出窗口中选择Ubuntu 20.04,指定nvm安装的Node路径(通常为/home/你的用户名/.nvm/versions/node/v15.x.x/bin/node)
  • 确认NPM路径自动关联为同目录下的npm,保存设置

3. 修复文件系统事件监听问题

  • 打开File > Settings > Appearance & Behavior > System Settings > Synchronization
  • 勾选Use "safe" file watching选项
  • 若问题仍存在,调整文件监听阈值:
    1. 打开Help > Edit Custom VM Options
    2. 添加以下配置:
      -Didea.max.intellisense.filesize=5000
      -Didea.filewatcher.threshold=2000
      
    3. 重启IDE

4. 清除IDE缓存并重启

  • 点击File > Invalidate Caches...
  • 勾选Clear file system cache and local history,点击Invalidate and Restart

5. 检查WSL与IDE兼容性

  • 通过wsl --list --verbose命令确认WSL 2已启用(Win11默认启用)
  • 将IntelliJ IDEA升级至2022.2.3及以上版本(该版本修复了部分WSL路径处理bug)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.22 11:45:34