Next.js + Firebase Functionsが同居するリポジトリでnext buildを実行するとエラーが発生

公開日時

Next.js + Firebase Functionsが同居するリポジトリで next build を実行したところ、下記のエラーが発生した。

Type error: Cannot find module 'xxx' or its corresponding type declarations.

今回、Next.jsもFirebase FunctionsもTypescriptで開発を行っており、Functionsのコードは ./functions 以下で管理するようにしていた。

tree -L 1
.
├── functions
│   ├── package.json
│   ├── src
│   └── tsconfig.json
├── tsconfig.json
├── package.json
└── pages

package.json がNext.jsとFirebase Functionsで別管理になっているにも関わらず、 next build 時にfunctions以下のコードもビルドしようとしたため Cannot find module エラーが発生していた。

そこでNext.js用の tsconfig.jsonexclude 設定を追加して、functions 以下を除外するようにした。

{
  "compilerOptions": {
    // ...
  },
  "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"],
  "exclude": ["node_modules", "functions"]
}

これでエラーなく next build が実行できるようになった。

参考


Related #next.js

Next.js使用時にrecoil-persistのStorageを変更する

クライアント側で実行された場合のみstorageを指定するようにした

Next.jsで環境に応じて特定のページを非表示にする

NODE_ENVでredirectsを出し分けた

Next.jsで動的URLをRewriteする

next.config.jsにrewritesを追加

Next.js + typescriptでpathsのエイリアスがModule not foundになる

next.config.jsに追記する必要があった

Next.js + Algoliaで全文検索UIを実装する

react-instantsearchを利用