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
- Follow our Code of Conduct
- Read the Contributing Guidelines.
- Read the docs.
- Check that there isn’t already an issue that reports the same bug to avoid creating a duplicate.
- Make sure this is a Vite issue and not a framework-specific issue. For example, if it’s a Vue SFC related bug, it should likely be reported to vuejs/core instead.
- Check that this is a concrete bug. For Q&A open a GitHub Discussion or join our Discord Chat Server.
- The provided reproduction is a minimal reproducible example of the bug.
About this issue
- Original URL
- State: closed
- Created a year ago
- Reactions: 22
- Comments: 48 (3 by maintainers)
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 nodesudo npm install n -gAfterwards I rannpm run devand 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 functionError with ViteIssue: Running
npm run devcaused the following error:TypeError: crypto.hash is not a function
Vite
v7.xand above use the newcrypto.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:
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
Installed supported Node version in your vscode terminal or mac terminal:
bash: nvm install 22.16.0 nvm use 22.16.0
Reinstalled dependencies:
bash : rm -rf node_modules package-lock.json npm install
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 triednpm run dev, and I get this errorI’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
just type npm i node
Here’s the command to update to the latest node if anyone wants:
nvm install 22+1
Here’s a patch if you guys still need to stay on a previous Node version for whatever reasons:
This actually , works thanks 😁👌
Thanks , it works