运行yarn dev启动Next.js项目失败,提示node.exe未识别
解决yarn启动Next.js时提示'node.exe'未识别的问题
问题描述
运行yarn dev启动Next.js项目时出现如下错误:
& : The term 'node.exe' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. At C:\Users\nayak\AppData\Roaming\npm\yarn.ps1:24 char:7 + & "node$exe" "$basedir/node_modules/yarn/bin/yarn.js" $args + ~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (node.exe:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException
解决方案
1. 检查Node.js路径是否在系统环境变量PATH中
- 右键「此电脑」→「属性」→「高级系统设置」→「环境变量」
- 在「系统变量」区域找到
PATH变量,查看是否包含Node.js的安装路径(64位默认路径为C:\Program Files\nodejs\) - 如果没有该路径,点击「新建」,粘贴Node.js的安装目录路径,依次点击「确定」保存所有设置
2. 重启终端/PowerShell
环境变量修改后,必须关闭所有已打开的终端窗口(包括PowerShell、命令提示符),重新打开后再执行yarn dev
3. 验证Node.js全局调用是否正常
在新终端中运行node -v,如果能正常输出版本号,说明环境变量配置生效;如果还是报错,重复步骤1检查路径是否正确
4. 修正yarn的node路径配置(可选)
如果上述步骤无效,可能是yarn的node路径配置错误:
- 执行
yarn config get nodePath查看当前配置的node路径 - 如果路径不正确,执行
yarn config set nodePath "C:\Program Files\nodejs\node.exe"(替换为你的实际Node.exe路径)
5. 针对nvm管理Node.js的情况(可选)
如果用nvm管理多个Node版本:
- 执行
nvm list查看已安装的版本及当前激活版本 - 执行
nvm use <你的Node版本号>切换到正确的版本,再运行yarn dev
内容的提问来源于stack exchange,提问作者Sthiti Pragyan Nayak
相关产品推荐
相关产品推荐

