Sphere
Log in Join

Vite 5 'crypto.hash is not a function' on Node 18.19 build

Our build box runs whatever Node 18 the base image happens to ship, which is how we learned about crypto.hash. A routine rebuild of a Vite 5.4.8 project started failing with:

error during build:
TypeError: crypto.hash is not a function
at getHash (file:///app/node_modules/vite/dist/node/chunks/dep-59dc6e00.js:42345:16)
at Object.renderChunk (file:///app/node_modules/vite/dist/node/chunks/dep-59dc6e00.js:44120:33)
at file:///app/node_modules/rollup/dist/es/shared/node-entry.js:18902:40

Dev server was fine. Only vite build failed, and only after assets got to the hashing stage.

## Where crypto.hash comes from

crypto.hash() is a helper added to Node's node:crypto module in Node 20.12.0 and backported to Node 18.20.0. Vite 5.4.x uses it to hash chunk names, and it doesn't guard the call — if the function isn't there, it throws. Our image was on Node 18.19.1, one minor version below the backport.

That's the whole bug: a dependency used an API from a newer release than the runtime had, and nobody declared the minimum version clearly enough. Vite 5's docs say Node 18+ and technically that's true for most of the tool, just not this part.

## Check your versions

bash
node --version # v18.19.1
npx vite --version # vite/5.4.8 linux-x64 node-v18.19.1

If you're at 18.19.x, this will bite you on the first build that needs to hash a chunk. Which is basically every build.

## Fix one: upgrade Node

The clean fix is a runtime that actually has the API:

bash
# nvm
nvm install 18.20.4
nvm use 18.20.4

# or in CI, pin the setup action
# actions/setup-node@v4 with node-version: '20.12.2'

We moved the image to Node 20.12.2, which is the LTS line and has crypto.hash() natively. If you're stuck on 18, 18.20.4 works too. I'd go to 20 unless something in the dependency tree forbids it.

## Fix two: pin Vite back

If upgrading Node isn't an option this week, downgrade Vite to a version before the crypto.hash change:

bash
npm install -D vite@5.3.5

Vite 5.3.x used the older createHash path and runs fine on 18.19. This is a temporary measure — you're pinning yourself away from a year of fixes — but it unblocks a release.

## Why dev didn't catch it

vite dev serves modules unbundled and doesn't produce hashed output filenames, so the hashing code path never runs. That's why this class of bug always shows up in vite build or in a preview deploy. If your CI only runs the dev server for smoke tests, add a real build step. We do npm run build && npx vite preview now, which would have caught this on the PR instead of on the release branch.

Also worth setting an engine field so this is explicit rather than folklore:

json
{ "engines": { "node": ">=20.12.0" } }

Some CI runners ignore it, but at least a local npm install warns you.

Turns out the dependency update that introduced this was a lockfile refresh, not an intentional Vite bump, which is why nobody connected the two at first.