You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

Windows11下npx create-next-app@latest报ENOENT错误求助

问题:Windows 11 PowerShell中运行npx create-next-app@latest报ENOENT错误

运行命令时出现以下错误输出:

PS C:\Users\hello\Desktop\SmartClass> npx create-next-app@latest
npm error code ENOENT
npm error syscall spawn C:\WINDOWS\system32\
npm error path C:\Users\hello\Desktop\SmartClass
npm error errno -4058
npm error enoent spawn C:\WINDOWS\system32\ ENOENT
npm error enoent This is related to npm not being able to find a file.
npm error enoent
npm error A complete log of this run can be found in: C:\Users\hello\AppData\Local\npm-cache\_logs\2025-04-11T13_21_56_292Z-debug-0.log

环境信息

  • 操作系统:Windows 11
  • Node.js:v22.14.0
  • npm:11.3.0
  • npx:11.3.0
  • 终端:PowerShell
  • 工作目录C:\Users\hello\Desktop\SmartClass

版本验证命令及结果:

PS C:\Users\hello\Desktop\SmartClass> npx -v
11.3.0
PS C:\Users\hello\Desktop\SmartClass> npm -v
11.3.0
PS C:\Users\hello\Desktop\SmartClass> node -v
v22.14.0

已尝试的解决方法

  • 多次通过nodejs.org官方安装包卸载重装最新版v22.14.0
  • 检查PATH环境变量,确认已包含C:\Program Files\nodejs\C:\Users\hello\AppData\Roaming\npmnode -vnpm -v可正常运行
  • 以管理员身份启动PowerShell,错误仍存在
  • 执行npm cache clean --force清理缓存,无效

观察到的现象

  • 错误提及的C:\WINDOWS\system32是目录而非可执行文件,怀疑npx错误地尝试调用shell或命令
  • 无法从调试日志中定位问题

问题

  1. 为何npx会尝试在C:\WINDOWS\system32\生成进程并触发ENOENT错误?
  2. 如何修复该问题使npx create-next-app@latest正常运行?
  3. Node.js v22.14.0、npm 11.3.0或npx在Windows11上是否存在已知相关问题?

解决方案

1. 修复npm的shell配置

npx在Windows下依赖系统shell执行命令,错误指向目录而非可执行文件,大概率是shell配置异常:

  • 打开PowerShell,查看当前shell配置:
    npm config get shell
    
  • 如果返回C:\WINDOWS\system32\或空值,执行以下命令设置正确的PowerShell路径:
    npm config set shell "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe"
    
  • 再次验证配置:
    npm config get shell
    
    确认返回完整的powershell.exe路径后,重新运行npx create-next-app@latest

2. 降级Node.js到LTS稳定版

Node.js v22属于最新版本,可能存在Windows环境下的兼容性问题,建议切换到长期支持(LTS)版本:

  1. 卸载当前Node.js:打开「设置」→「应用」→「应用和功能」,找到Node.js并卸载。
  2. 前往Node.js官网下载LTS版本(如v20.x)的Windows安装包,安装时务必勾选「Add to PATH」选项。
  3. 重启PowerShell,验证版本后重新尝试创建项目。

3. 用npm全局安装替代npx

若npx问题无法快速解决,可通过npm全局安装create-next-app:

  • 执行全局安装命令:
    npm install -g create-next-app
    
  • 安装完成后直接运行:
    create-next-app@latest
    

4. 清理PATH环境变量中的无效条目

PATH中存在异常条目可能干扰npx的进程调用:

  1. 右键「此电脑」→「属性」→「高级系统设置」→「环境变量」。
  2. 在「系统变量」中找到Path,点击「编辑」。
  3. 检查是否存在C:\WINDOWS\system32\这样的单独目录条目,若有则删除或调整其位置。
  4. 重启PowerShell后重试命令。

相关问题说明

Node.js v22和npm 11.x在Windows 11上存在部分已知兼容性反馈,主要集中在shell调用、权限相关的异常,降级到LTS版本是社区推荐的临时解决方案。

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

火山引擎 最新活动