React项目移动组件文件后出现Module not found导入异常问题排查
Let's break down what's causing this error and how to fix it:
Your project structure places App.js (or App.jsx) directly under the src/ directory, while your EditOrCreateUser.jsx lives in src/Components/CreateEditUser/.
The import line you’re currently using:
import { PropContext } from "../App";
only moves one level up from your component’s directory, landing you in src/Components/—but App.js isn’t located there, it’s one level higher in the root src/ folder.
The Immediate Fix
Update the import path to use two levels of relative navigation to reach the src/ directory:
import { PropContext } from "../../App";
../../ will take you from src/Components/CreateEditUser/ → src/Components/ → src/, where App.js is stored.
Why This Happened
Before you rearranged your component folders, the relative path to App.js was correct. Now that you’ve nested EditOrCreateUser one level deeper in the CreateEditUser subfolder, the path needs an extra ../ to reach the parent src/ directory.
Additional Troubleshooting (If the Fix Doesn’t Work Right Away)
- Double-check filename accuracy: Ensure
App.js(orApp.jsx) matches the name in your import exactly—build tools like Webpack are case-sensitive, even on Windows systems. - Verify the context export: Confirm
App.jsproperly exportsPropContextwith a line likeexport const PropContext = React.createContext(...). - Restart your dev server: Webpack’s cache can sometimes hold onto old path references, so a fresh restart will clear this up.
内容的提问来源于stack exchange,提问作者VeljkoS

