4
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Cannot find module 'next/dist/next-server/lib/router-context'の解決方法

Last updated at Posted at 2021-08-12

概要

Next.jsでビルド時に以下のエラーが出て困ったので、解決方法を残しておきます。

Type error: Cannot find module 'next/dist/next-server/lib/router-context' or its corresponding type declarations.

結論

RouterContextのimportパスをnext/dist/shared/lib/router-contextに変更します。

// good
import { RouterContext } from "next/dist/shared/lib/router-context";
// bad
import { RouterContext } from "next/dist/next-server/lib/router-context"; 

自分はこれでエラーがなくなりました。

なぜ起こったか

Next.js@11.1でrouter-contextの置き場所が変わったようです。

Storybookのドキュメントにも以下のように書いてあります。

import { RouterContext } from "next/dist/shared/lib/router-context"; // next 11.2
import { RouterContext } from "next/dist/next-server/lib/router-context"; // next < 11.2


export const parameters = {
  nextRouter: {
    Provider: RouterContext.Provider,
  },
}

ここにはnext < 11.2と書いてありますが、Next.js公式は11.1でパスが変わったよ!と言ってるので、11.1でもパスを変更しましょう。

依存アップデートしたらエラーが出た!という人の参考になれば幸いです。

参考

4
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?