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

FigmaでSVG Octocatを作る その3

アウトライン化大事

Next.jsプロジェクトのGoogle Analytics V3をV4に更新する

トラッキングIDの差し替えのみで対応が完了した

Vercelで指定のブランチのみデプロイを実行する

Ignored Build Stepを設定した