vite: TypeError: crypto.hash is not a function

Describe the bug

I simply installed a React TypeScript project using pnpm create vite and tried to run it, but encountered the following error. I’m on a Windows environment and using Node.js version v20.9.0. This issue didn’t occur before, so it seems to be related to the recent update to Vite version 7.0.

G:\project>pnpm create vite
|
o  Project name:
|  vite-sample
|
o  Select a framework:
|  React
|
o  Select a variant:
|  TypeScript
|
o  Scaffolding project in G:\project\vite-sample...
|
—  Done. Now run:

  cd vite-sample
  pnpm install
  pnpm run dev


G:\project>cd vite-sample

G:\project\vite-sample>pnpm install
Packages: +188
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Progress: resolved 232, reused 188, downloaded 0, added 188, done

dependencies:
+ react 19.1.0
+ react-dom 19.1.0

devDependencies:
+ @eslint/js 9.29.0
+ @types/react 19.1.8
+ @types/react-dom 19.1.6
+ @vitejs/plugin-react 4.6.0
+ eslint 9.29.0
+ eslint-plugin-react-hooks 5.2.0
+ eslint-plugin-react-refresh 0.4.20
+ globals 16.2.0
+ typescript 5.8.3
+ typescript-eslint 8.35.0
+ vite 7.0.0

╭ Warning ───────────────────────────────────────────────────────────────────────────────────╮
│                                                                                            │
│   Ignored build scripts: esbuild.                                                          │
│   Run "pnpm approve-builds" to pick which dependencies should be allowed to run scripts.   │
│                                                                                            │
╰────────────────────────────────────────────────────────────────────────────────────────────╯

Done in 2.7s using pnpm v10.5.1

G:\project\vite-sample>pnpm run dev

> vite-sample@0.0.0 dev G:\project\vite-sample
> vite

error when starting dev server:
TypeError: crypto.hash is not a function
    at getHash (file:///G:/project/vite-sample/node_modules/.pnpm/vite@7.0.0/node_modules/vite/dist/node/chunks/dep-Bsx9IwL8.js:2834:21)
    at getLockfileHash (file:///G:/project/vite-sample/node_modules/.pnpm/vite@7.0.0/node_modules/vite/dist/node/chunks/dep-Bsx9IwL8.js:11716:9)
    at getDepHash (file:///G:/project/vite-sample/node_modules/.pnpm/vite@7.0.0/node_modules/vite/dist/node/chunks/dep-Bsx9IwL8.js:11719:23)
    at initDepsOptimizerMetadata (file:///G:/project/vite-sample/node_modules/.pnpm/vite@7.0.0/node_modules/vite/dist/node/chunks/dep-Bsx9IwL8.js:11180:53)
    at createDepsOptimizer (file:///G:/project/vite-sample/node_modules/.pnpm/vite@7.0.0/node_modules/vite/dist/node/chunks/dep-Bsx9IwL8.js:34593:17)
    at new DevEnvironment (file:///G:/project/vite-sample/node_modules/.pnpm/vite@7.0.0/node_modules/vite/dist/node/chunks/dep-Bsx9IwL8.js:35357:109)
    at Object.defaultCreateClientDevEnvironment [as createEnvironment] (file:///G:/project/vite-sample/node_modules/.pnpm/vite@7.0.0/node_modules/vite/dist/node/chunks/dep-Bsx9IwL8.js:35776:9)
    at _createServer (file:///G:/project/vite-sample/node_modules/.pnpm/vite@7.0.0/node_modules/vite/dist/node/chunks/dep-Bsx9IwL8.js:28359:132)
    at async CAC.<anonymous> (file:///G:/project/vite-sample/node_modules/.pnpm/vite@7.0.0/node_modules/vite/dist/node/cli.js:573:18)
 ELIFECYCLE  Command failed with exit code 1.

Reproduction

.

Steps to reproduce

No response

System Info

windows, node v20.9.0

Used Package Manager

pnpm

Logs

No response

Validations

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Reactions: 22
  • Comments: 48 (3 by maintainers)

Most upvoted comments

I encountered the same problem, I had a version 21 of node and when I upgraded to version 22.16.0 I deleted the node_module files, the package-lock.json and I reinstalled the dependencies, it now works without any problem

I had the same error and I was able to fix mine by running: npm install node sudo npm install n -g Afterwards I ran npm run dev and it runs just fine.

With Vite 7, the minimum required node versions has been bumped as mentioned in the comments above. You need to use Node.js 20.19+ or 22.12+. The package manager should usually warn about this beforehand.

I got the same error using Node v20.11.0 I updated node v as @DanielMwamba suggested, and it worked

i just upgraded to node version 22.16.0 and it worked

+1 here

for me I just run npm create vite@latest (v7.0.0)

and then when I run the app, I got the exact same error

25 years of web development, and JS: a constant source of trouble, yesterday, today, and tomorrow… This JS is and always will be the biggest pain in the ass I’ve ever known!

✅ Fix: crypto.hash is not a function Error with Vite

Issue: Running npm run dev caused the following error:

TypeError: crypto.hash is not a function

Vite v7.x and above use the new crypto.hash() API, which is only available in:

Node.js v20.19.0+ Node.js v22.12.0+

Only works if your version of Node is v18.20.4, which does not support this method.

✅ Solution:

  1. Install NVM (Node Version Manager) if not installed:

    bash: curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash source ~/.zshrc

  2. Installed supported Node version in your vscode terminal or mac terminal:

    bash: nvm install 22.16.0 nvm use 22.16.0

  3. Reinstalled dependencies:

    bash : rm -rf node_modules package-lock.json npm install

  4. Ran dev server successfully:

    bash: npm run dev

Result: The Vite dev server started without any errors. Tailwind CSS and React are now working correctly with no version conflicts.

it worked, thanks @DanielMwamba and @leomonzonspaceguru !

I deleted node_modules and package.lock.json and reinstalled it again, but still the same errors. Node V22.17.0 Windows.

same using the new vite engine, run ```npm install node, fixes the issue

+1 here I tried npm create vite@latest (v 7.0.0), and tried npm run dev, and I get this error

I’m on Linux (Ubuntu) using Hyprland.

if upgrading node js to 20+ did’nt work, try downgrading vite to vite@6 and reinstall node modules

I encountered the same problem, I had a version 21 of node and when I upgraded to version 22.16.0 I deleted the node_module files, the package-lock.json and I reinstalled the dependencies, it now works without any problem

just type npm i node

Here’s the command to update to the latest node if anyone wants:

nvm install 22

Here’s a patch if you guys still need to stay on a previous Node version for whatever reasons:

diff --git a/dist/node/chunks/dep-Bsx9IwL8.js b/dist/node/chunks/dep-Bsx9IwL8.js
index 0583030d32a3a36aa13fbb2839435e5af6c0a9f4..43463dcb8c7a9cee2e44b29bc67902dcb360fa25 100644
--- a/dist/node/chunks/dep-Bsx9IwL8.js
+++ b/dist/node/chunks/dep-Bsx9IwL8.js
@@ -2830,8 +2830,16 @@ const singlelineCommentsRE = /\/\/.*/g;
 const requestQuerySplitRE = /\?(?!.*[/|}])/;
 const requestQueryMaybeEscapedSplitRE = /\\?\?(?!.*[/|}])/;
 const blankReplacer = (match) => " ".repeat(match.length);
+const hash =
+  // crypto.hash is supported in Node 21.7.0+, 20.12.0+
+  crypto.hash ??
+  ((
+    algorithm,
+    data,
+    outputEncoding,
+  ) => crypto.createHash(algorithm).update(data).digest(outputEncoding))
 function getHash(text, length = 8) {
-	const h$2 = crypto.hash("sha256", text, "hex").substring(0, length);
+	const h$2 = hash("sha256", text, "hex").substring(0, length);
 	if (length <= 64) return h$2;
 	return h$2.padEnd(length, "_");
 }

I encountered the same problem, I had a version 21 of node and when I upgraded to version 22.16.0 I deleted the node_module files, the package-lock.json and I reinstalled the dependencies, it now works without any problem

This actually , works thanks 😁👌

I encountered the same problem, I had a version 21 of node and when I upgraded to version 22.16.0 I deleted the node_module files, the package-lock.json and I reinstalled the dependencies, it now works without any problem

Thanks , it works