【Rails】webpacker-dev-server実行中のエラー「Could not freeze ... Cannot read property 'hash' of undefined」

はじめに

webpacker-dev-server実行中に、突然以下のエラーが出力されました。

[hardsource:3154a74b] Could not freeze ./app/webpack/packs/application.js: Cannot read property 'hash' of undefined
(node:69641) UnhandledPromiseRejectionWarning: TypeError: current.readableIdentifier is not a function
    at formatError (/Users/user/Products/project-name/node_modules/webpack/lib/Stats.js:352:30)
    at Array.map (<anonymous>)
    at Stats.toJson (/Users/user/Products/project-name/node_modules/webpack/lib/Stats.js:359:31)
    at WebpackAssetsManifest.handleEmit (/Users/user/Products/project-name/node_modules/webpack-assets-manifest/src/WebpackAssetsManifest.js:445:41)
    at AsyncSeriesHook.eval [as callAsync] (eval at create (/Users/user/Products/project-name/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:5:1)
    at AsyncSeriesHook.lazyCompileHook (/Users/user/Products/project-name/node_modules/tapable/lib/Hook.js:154:20)
    at Compiler.emitAssets (/Users/user/Products/project-name/node_modules/webpack/lib/Compiler.js:491:19)
    at onCompiled (/Users/user/Products/project-name/node_modules/webpack/lib/Watching.js:51:19)
    at /Users/user/Products/project-name/node_modules/webpack/lib/Compiler.js:681:15
    at _next0 (eval at create (/Users/user/Products/project-name/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:14:1)
(node:69641) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:69641) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

2行目以降が出力されるのは稀で、大抵は1行目だけ単一行または複数行出力され、そのときはページが表示されなくなるなどの影響はありません。

原因を調べましたが結局よくわからず、とりあえず対処の方法だけ記事にしておきます。

対処

webpack-dev-serverを実行中の場合は一旦停止します。その後、以下のコマンドを実行してキャッシュを削除します。

$ rm -rf node_modules/.cache/hard-source/

再度webpack-dev-serverを実行すればエラーは解消されているはずです。

関連記事

【Rails】Railsアップグレードまとめ
# はじめに Ruby on Railsに限らず、何らかのフレームワークを使ってWebシステムを構築している場合、フレームワークのアップグレード作業は避けて通れません。 一般的にフレームワークはバージョン毎にEOL (End of Life [...]
2022年10月1日 14:32
【Rails】ユーザー登録時に行うメールアドレス認証機能の実装方法
# はじめに ユーザー登録/解除やログイン/ログアウトといった認証機能の導入に「devise」というGemを使っている人は多いと思います。「devise」では以下のように記述するだけで、ユーザー登録時に確認メールを送付しメールアドレス認証を行う機 [...]
2022年9月24日 14:24
【Rails】モデルに列挙型(enum)を定義し、使いこなす方法
# はじめに Railsはモデルでカラム名と同名の列挙型(enum)を定義することで、カラムと列挙型の変数を紐付けることができます。カラムと列挙型の変数を紐付けると、カラムに対して様々な便利な使い方ができるようになります。 本記事では、モデ [...]
2022年9月3日 10:29
【Rails】RailsでCORSとPreflight requestの設定を行う方法
# はじめに RailsアプリをAPIサーバーとして構築するには、CORS (Cross-Origin Resource Sharing)と Preflight requestの設定を行う必要があります。APIサーバーは外部からの要求に対して処理 [...]
2022年8月27日 10:44
【Ruby】Bundlerを使ってRubyGemsを作成/公開する方法
# はじめに Bundlerを使ってRubyGemsを作成および公開する方法について説明します。Bundlerを使わずにRubyGemsを作成/公開する方法については以下の記事を参照してください。 <iframe class="hatena [...]
2022年7月12日 23:18
【Ruby】RubyGemsを作成/公開する方法
# はじめに RubyGemsを作成および公開する方法について説明します。Bundlerを使ってRubyGemsを作成する方法については以下の記事を参照してください。 <iframe class="hatenablogcard" style [...]
2022年7月11日 21:52
【Rails】M1チップ搭載MacでRuby on Railsの開発環境構築
# はじめに M1チップ搭載MacにRuby on Railsの開発環境を構築する手順を記載します。 - MacBook Air (M1, 2020) - macOS Monterey 12.3.1 # Homebrew ## [...]
2022年5月5日 11:56
【Rails】Rakeタスクの基本情報と作成・実行方法
# はじめに Railsには標準でRakeというGemが同梱されています。RakeはRubyで実装されたMake(UNIX系のOSで使用できるコマンド)のようなビルド作業を自動化するツールです。Ruby Make、略してRakeというわけですね。 [...]
2022年3月7日 22:12