Maaz Zindani

Loading the index…

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

Link cannot be imported from react-router

Errorexport 'Link' (imported as 'Link') was not found in 'react-router'

Short answer

Which package exports Link depends on your React Router major version. In v5 and v6, Link lives in react-router-dom, not react-router. In v7 and v8, Link is imported from react-router. The error means your import and your installed version disagree, so print the version and match the two.

  • Applies to react-router 6.30, react-router 7, React 19
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    List the installed React Router packages. The major version decides which import line is correct.

    npm ls react-router react-router-dom
  2. 2Match what you see, then jump to its fix

    1. 1npm ls shows react-router 5.x or 6.x, and the failing line is import { Link } from 'react-router'You are on v5 or v6 and import Link from react-routerFix
    2. 2Your imports say from 'react-router' (as in the v7 docs) while npm ls shows 6.xCode was written for v7 but react-router is still on v6Fix
    3. 3Import from 'react-router-dom' fails after upgrading, or the package is missingYou left react-router-dom behind while moving to v8Fix

What does the error look like?

  • export 'Link' (imported as 'Link') was not found in 'react-router'
  • 'react-router' does not contain an export named 'Link'

Cause 1 of 3

You are on v5 or v6 and import Link from react-router

1Check

Search for imports of the core package. In the v6 upgrade guide, the web components such as Link and BrowserRouter are imported from react-router-dom.

grep -rn "from ['\"]react-router['\"]" src/

2Fix

Install the web package if it is missing and import the web components from it.

npm install react-router-dom@6

// before
import { Link } from "react-router";
// after
import { Link, BrowserRouter } from "react-router-dom";

3Verify

Restart the dev server. The build no longer reports a missing export.

npm run build

Confirmed by reactrouter.com

Did this fix it?

Cause 2 of 3

Code was written for v7 but react-router is still on v6

1Check

The v7 upgrade page imports Link and useLocation from react-router. If package.json still pins an older major, that import has nothing to find.

grep -n "react-router" package.json

2Fix

Move to a current release, then keep the imports from react-router. The upgrade page gives this command for v7 and says to update to the latest v7 first.

npm install react-router@7
// import { Link, useLocation } from "react-router";

3Verify

Check that one react-router version is installed and the app builds.

npm ls react-router
npm run build

Confirmed by reactrouter.com

Did this fix it?

Cause 3 of 3

You left react-router-dom behind while moving to v8

1Check

The v7 upgrade page says v8 removes the react-router-dom re-export package. Look for leftover imports and for the dependency.

grep -rn "react-router-dom" src/ package.json

2Fix

Uninstall react-router-dom, import everything from react-router, and import DOM-specific APIs such as RouterProvider from react-router/dom.

npm uninstall react-router-dom

import { Link, useLocation } from "react-router";
import { RouterProvider } from "react-router/dom";

3Verify

Search again: no file should import react-router-dom.

grep -rn "react-router-dom" src/ || echo "none left"

Confirmed by reactrouter.com

Did this fix it?

How do I stop it coming back?

  • Read the docs version selector before copying an import line: the right package name changed between v6 and v7.
  • Keep one React Router package family in package.json, not react-router and react-router-dom at different majors.
  • Run the build in CI so a bad import fails before deploy.

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.