Import and export may only appear at the top level

The error "import and export may only appear at the top level" may occur due to a missing closing brace when declaring a function or class or mixing up default and named exports and imports.

Let's take an example of the below code.

// This code throws error
function App() {
  	return (
  		<div>
    		<p>Hi there!</p>    	
    	</div>
  	);
// Here we have missing closing braces
export default App;

In the above code, you may notice that the closing braces of the app function were missing. This caused the above error. We can add the closing braces and thus the error was resolved in the below code. The same error will be shown even if we are working with class components.

Solution:

function App() {
  	return (
  		<div>
    		<p>Hi there!</p>    	
    	</div>
  	);
}

export default App;

 

How to find the missing braces in the code

You can find such errors very quickly if you are using any modern IDE like visual studio code or simply VSCode. It will show the error that a curly brace is expected at the end. VSCode will show one error and if you hover the cursor over it, it will show a message like “The parser expected to find a ‘}’ to match the ‘{’”. The code formatting (prettier or the default code format) will not work if we have braces missing in the code.

Conclusion

To fix React error “import and export may only appear at the top level”, check whether we have any missing closing braces in our code.

 

Related Blogs

ckeditor

How to Integrate Custom Build CKEditor5 with React

Explains how to create a custom build CKEditor5 and integrate with react with sample code.

Objects Are Not Valid as a React Child

React objects are not valid as a react child

The React error "Objects are not valid as a React child" occurs when we render an object or an array in our JSX code. We can't use JavaScript objects as a child in React.

Can't Perform a React State Update on an Unmounted Component

Can't Perform a React State Update on an Unmounted Component

React Warning “Can't perform a react state update on an unmounted component” is caused when we try to update the state after the component was unmounted. Explains how to fix it.

Each child in a list should have a unique key prop

Each child in a list should have a unique key prop

Solve Warning: Each child in a list should have a unique ”key" prop in React by setting the id property as a unique key or by auto-assigning unique keys.

React Hook is Called Conditionally

React Hook is Called Conditionally

Error: "React Hook is called conditionally. React Hooks must be called in the exact same order in every component render" occurs when hooks are invoked conditionally or after a return of a value.

Too many re-renders. React limits the number of renders to prevent an infinite loop

Too many re-renders. React limits the number of renders to prevent an infinite loop

The React error "Too many re-renders. React limits the number of renders to prevent an infinite loop" happens when you have reached an infinite render loop.

Adjacent JSX elements must be wrapped in an enclosing tag

Adjacent JSX elements must be wrapped in an enclosing tag

The "Adjacent JSX elements must be wrapped in an enclosing tag" error can be solved by wrapping the multiple elements in a parent div or in a react fragment

The tag is unrecognized in this browser

Warning: The tag is unrecognized in this browser

Warning "The tag is unrecognized in this browser" occurs when the tag doesn't exist in the browser or when we render a component starting with a lowercase letter.

The style prop expects a mapping from style properties to values, not a string

The style prop expects a mapping from style properties to values, not a string

"The style prop expects a mapping from style properties to values, not a string" React error occurs when we pass a CSS string to the style attribute.