下载的React模板无法运行,执行npm run start报错求助
错误信息翻译
npm ERR! 代码 ENOENT
npm ERR! 系统调用 open
npm ERR! 路径 C:\Users\hp\Desktop\ReactIntroduction\material-kit-react-main\package.json
npm ERR! 错误码 -4058
npm ERR! enoent 无法读取package.json:错误:ENOENT: 没有该文件或目录,打开 'C:\Users\hp\Desktop\ReactIntroduction\material-kit-react-main\package.json'
npm ERR! enoent 这与npm无法找到文件有关。
npm ERR! enoent
排查与解决步骤
确认终端工作路径
打开终端,运行cd(Windows)或pwd(Linux/macOS),检查当前目录是否为C:\Users\hp\Desktop\ReactIntroduction\material-kit-react-main。如果不是,执行cd C:\Users\hp\Desktop\ReactIntroduction\material-kit-react-main切换到目标目录后再重试命令。检查package.json是否存在
手动打开material-kit-react-main文件夹,确认里面有没有package.json文件。如果没有,说明模板下载不完整,要么重新下载完整的模板包,要么在目标目录运行npm init -y生成默认的package.json,之后手动添加start脚本:"scripts": { "start": "react-scripts start" }若使用react-scripts,需先执行
npm install react-scripts --save安装依赖。不要手动复制node_modules
直接复制node_modules容易导致依赖版本不匹配、路径错误。正确做法是确认package.json存在后,在目标目录运行npm install,让npm自动生成完整且匹配的node_modules和package-lock.json。检查文件夹权限
确保当前用户对material-kit-react-main文件夹有读写权限。Windows下可右键文件夹→属性→安全,检查并添加当前用户的完全控制权限。
内容的提问来源于stack exchange,提问作者Kevin Sirengo

