2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

[React]error when starting dev server: TypeError: crypto.hash is not a function

Posted at

問題

npm run dev を実行するとタイトルのエラーが出た。

error when starting dev server: TypeError: crypto.hash is not a function

これはViteが依存関係の最適化を行う際に、ファイルのハッシュを計算しようとして crypto.hash という存在しない関数を誤って呼び出していることが原因です。
使用しているNode.jsのバージョンや、Vite自身のバージョンと依存関係のミスマッチによって引き起こされる可能性が高いです。

解決方法

Viteは特定のNode.jsバージョンに依存しています。古いバージョンや、Viteが意図しない新しいNode.jsのバージョンを使用していると、このような問題が発生することがあります。Viteの推奨Node.jsバージョンは、通常、最新のLTS(Long Term Support)バージョンです。

なので、

最新のNodejsをインストールする

今回は nvm を使用して最新のNode.jsをインストールします。
私の環境は WSL 上でVite環境を構築しているのでLinux環境でnvm をインストールします。

  • nvm のインストール
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
  • シェルに nvm をロードする
source ~/.bashrc

※シェルは起動時に設定ファイル(.bashrc)などを読み込みます。すでに起動中の状態ではnvmコマンドは自動的に読み込まれないので明示的に読み込ませる必要があります。

  • Node.jsの最新LTS (Long Term Support) バージョンをインストールする
nvm install --lts
  • インストールされたバージョンを確認する
node -v
npm -v

これでもう一度、npm run dev をするとエラーが直り、画面が表示されました。

おわりに

ViteのエラーはNode.jsが関係していることを覚えておく

2
0
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
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?