question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cannot use default (singleton) router: "You should only use "next/router" on the client side of your app"

See original GitHub issue

Hello! First of all thanks for awesome package, @scottrippey!

I try to mock next-router with storybook. Is it real to mock default Router as we do it for useRouter?

For instance useRouter works fine:

useRouter example
import { useCallback } from 'react'
import { useRouter } from 'next/router'

const UseRouterPage = () => {
  const router = useRouter()

  const handleClick = useCallback(() => router.push({
      pathname: router?.pathname,
      query: {
        ...router?.query,
        test: '1'
      },
    }, undefined,
    {
      scroll: false,
      shallow: true
    }), [])

  return (
    <main>
      <button onClick={handleClick}>button</button>
    </main>
  )
}

export default UseRouterPage

But with default exported Router, the next error appears:

image

I try use default exported Router because in this case there is no component re-render.

useRouter example
import { useCallback } from 'react'
import Router from 'next/router'

const RouterPage = () => {
  const handleClick = useCallback(() => Router.push({
    pathname: Router?.pathname,
    query: {
      ...Router?.query,
      test: '1'
    },
  }, undefined,
  {
    scroll: false,
    shallow: true
  }), [])

  return (
    <main>
      <button onClick={handleClick}>button</button>
    </main>
  )
}

export default RouterPage

Repo to reproduce: https://github.com/sedlukha/next-router-mock-example

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:19 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
payable-saul-liangcommented, Nov 10, 2022

@amolagre I had the same ModuleNotFoundError error as you, but it seems to work by importing the next-12 version specifically:

import { MemoryRouterProvider } from "next-router-mock/MemoryRouterProvider/next-12";
1reaction
amolagrecommented, Sep 26, 2022

Hi @scottrippey , Any update to make next/link (12.2) working with storybook. I’m getting this error while running npm run storybook

ModuleNotFoundError: Module not found: Error: Can't resolve 'next/dist/next-server/lib/router-context' in 'my-project\node_modules\next-router-mock\dist\MemoryRouterProvider'

Read more comments on GitHub >

github_iconTop Results From Across the Web

No router instance found. You should only use "next ... - GitHub
You should only use "next/router" inside the client side of your app. #6713 ... import Router from 'next/router' export default (props) ...
Read more >
import Router from 'next/router' is it ok? - Stack Overflow
You will receive an error with: You should only use "next/router" inside the client side of your app . This is because of...
Read more >
next/router | Next.js
useRouter is a React Hook, meaning it cannot be used with classes. You can either use withRouter or wrap your class in a...
Read more >
Next js router push without reload - Caritas Castellaneta
In this short article we'll cover how to redirect to internal and external pages, if you're using Vue Router and if you're just...
Read more >
next-router-mock - npm
Start using next-router-mock in your project by running `npm i ... If you want to mock next/link , you should also include this...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found