React组件开发编译报错:Module not found路径解析失败求助
Hey there, let's tackle the issues you're hitting step by step—first that annoying compile error, then a hidden problem that'll cause headaches later.
1. Resolve the "Module not found" Error
The root of your compile failure is a wrong relative import path. Let's map out your project structure clearly:
D:\Treebeard\ └── src\ └── components\ ├── file_tree.js └── directory.js
Your file_tree.js lives directly in the src/components/ folder. When you wrote:
import { Directory } from './components/directory.js';
You're telling React to look for a components subfolder inside your current components folder (which doesn't exist!). Since directory.js is in the same folder as file_tree.js, just use the current directory path:
// In file_tree.js, fix the import to: import { Directory } from './directory.js';
That'll resolve the "Module not found" error immediately.
2. Fix the Hidden Circular Dependency
Wait, there's another issue lurking here—your directory.js imports TriangleDown from file_tree.js, while file_tree.js imports Directory from directory.js. This creates a circular dependency, which React will warn about (or break entirely in some cases).
Here's the cleanest fix:
- Extract shared components like
TriangleDowninto a separate, standalone file. For example, createsrc/components/icons.js:import React from 'react'; export const TriangleDown = () => { // Add your TriangleDown component logic here return <span className="triangle-down">▼</span>; }; - Then update both files to import from this new shared file:
// In file_tree.js import { TriangleDown } from './icons.js';// In directory.js import { TriangleDown } from './icons.js';
This breaks the circular loop and keeps your code organized. If you can't extract TriangleDown for some reason, you could also refactor to pass the component as a prop instead of importing it directly.
Give these changes a try, and your components should compile and work as expected!
内容的提问来源于stack exchange,提问作者Timmy

