概要
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
でもパスを変更しましょう。
依存アップデートしたらエラーが出た!という人の参考になれば幸いです。