Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

Parsing error: Identifier 'React' has already been declared

ErrorParsing error: Identifier 'React' has already been declared

Short answer

An import binds its name in the file's scope, and no other declaration there may use the same name. So React is declared twice: two imports, or an import next to a const or require. Keep one. With the automatic JSX runtime you may not need the React import at all, and a codemod removes it.

  • Applies to React 19, Babel 7, Node.js 22
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    List every line in the failing file that binds the name React.

    grep -nE "import React|import \* as React|const React|let React|var React|require\('react'\)" path/to/File.jsx
  2. 2Match what you see, then jump to its fix

    1. 1The grep shows two lines, for example import React from 'react' and later import React, { useState } from 'react'React is imported or declared twice in one fileFix
    2. 2The project uses React 17 or newer with an automatic JSX runtime, and files import React only for JSXThe import is left over from the old JSX transformFix

What does the error look like?

  • Parsing error: Identifier 'React' has already been declared
  • SyntaxError: Identifier 'React' has already been declared

Cause 1 of 2

React is imported or declared twice in one file

1Check

MDN says an imported binding cannot be in the same scope as any other declaration, including let, const, class, function, var and another import.

grep -n "from 'react'" path/to/File.jsx

2Fix

Merge the lines into one import that carries every name you need.

// before
import React from 'react';
import React, { useState } from 'react';

// after
import React, { useState } from 'react';

3Verify

The parser stops reporting the error.

npx eslint path/to/File.jsx

Confirmed by developer.mozilla.org

Did this fix it?

Cause 2 of 2

The import is left over from the old JSX transform

1Check

The React team says the new transform imports what JSX needs by itself, so JSX no longer requires import React. Babel's automatic runtime does the same, while the classic runtime imports nothing and uses React.createElement.

grep -rn "runtime" babel.config.* .babelrc tsconfig.json vite.config.*

2Fix

Run the codemod that removes unused React imports and converts the rest to named imports, then keep only the named hooks you use.

npx react-codemod update-react-imports

3Verify

Run the app or build; JSX still compiles and the duplicate is gone.

npm run build

Confirmed by legacy.reactjs.org

Did this fix it?

How do I stop it coming back?

  • Let the editor remove unused imports on save, so duplicates do not pile up after a merge.
  • Set the JSX runtime to automatic in new projects.
  • Turn off react/react-in-jsx-scope and react/jsx-uses-react in ESLint when you use the automatic runtime.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.