The "Failed to compile" error appears in the browser overlay when your React app has a build-time error that prevents it from compiling. Unlike runtime errors, these must be fixed before the app can run at all. They are usually syntax errors, import issues, or TypeScript type errors.
// ❌ Common syntax errors
function App() {
return (
<div>
<h1>Hello</h1> // Missing closing tag
<div> // Wrong closing tag
)
}
// ✅ Fixed
function App() {
return (
<div>
<h1>Hello</h1>
</div>
);
}
// ❌ Multiple root elements without wrapper
return (
<h1>Title</h1>
<p>Content</p> // Error: Adjacent JSX elements must be wrapped
);
// ❌ Using class instead of className
<div class="container"> // Error in JSX
// ❌ Unclosed self-closing tag
<img src="photo.jpg"> // Must be self-closed in JSX
// ✅ Wrap in fragment or div
return (
<>
<h1>Title</h1>
<p>Content</p>
</>
);
// ✅ Use className in JSX
<div className="container">
// ✅ Self-close void elements
<img src="photo.jpg" alt="photo" />
// ❌ Wrong path
import Button from './components/button'; // File is Button.jsx
import styles from './App.css'; // File is App.module.css
import { helper } from '../utils'; // Missing file extension or index
// ✅ Match exact filename (case-sensitive on Linux/Mac)
import Button from './components/Button';
import styles from './App.module.css';
import { helper } from '../utils/helper';
// ❌ Arrow function with block body needs explicit return
const App = () => {
<div>Hello</div> // No return keyword!
}
// ❌ Parentheses on new line (ASI issue)
function App() {
return
<div>Hello</div> // Returns undefined due to ASI!
}
// ✅ Add return keyword
const App = () => {
return <div>Hello</div>;
}
// ✅ Or use implicit return with parentheses
const App = () => (
<div>Hello</div>
);
// ✅ Keep opening paren on same line as return
function App() {
return (
<div>Hello</div>
);
}
// ❌ Missing type annotation
function Greeting({ name }) { // Error: Parameter 'name' implicitly has 'any' type
return <h1>Hello {name}</h1>;
}
// ✅ Add type annotation
interface GreetingProps {
name: string;
}
function Greeting({ name }: GreetingProps) {
return <h1>Hello {name}</h1>;
}
It's a build-time error that prevents your app from compiling. Unlike runtime errors, the app won't load at all. Common causes include syntax errors, invalid JSX, wrong import paths, and TypeScript errors.
The error overlay shows the file path, line number, and column. Look for the red underline in your editor. The terminal running npm start also shows the full error with location.
Because class is a reserved keyword in JavaScript. JSX is compiled to JavaScript, so it uses className to avoid conflicts. Similarly, for becomes htmlFor.
JavaScript's Automatic Semicolon Insertion (ASI) adds a semicolon after return if the next line starts with a new statement. Always put the opening parenthesis on the same line as return.
Check the exact filename including case (Button.jsx vs button.jsx). Verify the file exists at the specified path. Use your editor's autocomplete for import paths to avoid typos.
Explore 500+ free tutorials across 20+ languages and frameworks.