vite build fails with exit code 127 on a hosting build
sh: vite: command not foundShort answer
Exit 127 means the shell found no vite program. The vite package lives in devDependencies, and npm skips those when NODE_ENV is production. Install with dev dependencies included, or run the install before the build. npm run adds node_modules/.bin to PATH, so a working install is all it needs.
Fix it fast
1Find out which cause you have
Look for the install step in the build log and check whether node_modules/.bin/vite exists after it.
echo "NODE_ENV=$NODE_ENV" ls node_modules/.bin/vite2Match what you see, then jump to its fix
What does the error look like?
sh: vite: command not foundError: Command "vite build" exited with 127
Cause 1 of 2
devDependencies were skipped because NODE_ENV is production
1Check
With NODE_ENV=production or --production, npm does not install devDependencies. The omit setting defaults to dev in that case.
npm config get omit
grep -n -A8 devDependencies package.json | grep vite2Fix
Install with dev dependencies included for the build step. The npm docs say an included type wins over an omitted one, and also document --production=false. Or move vite to dependencies if the build runs where dev packages are never installed.
npm install --include=dev3Verify
The binary exists and the build starts.
ls node_modules/.bin/vite
npm run buildConfirmed by docs.npmjs.com
Cause 2 of 2
No install ran, so node_modules is empty or absent
1Check
npm run puts node_modules/.bin on PATH, but only if the folder exists. When it does not, npm warns you to run npm install.
ls node_modules | head -3
grep -n "\"scripts\"" -A6 package.json2Fix
Add an install command before the build command in the pipeline.
npm install
npm run build3Verify
The build script finds vite.
npm run buildConfirmed by docs.npmjs.com
How do I stop it coming back?
- Run the build as npm run build, not as a bare vite command, so node_modules/.bin is on PATH.
- Keep NODE_ENV=production for the runtime, and set it only after the install and build in a Dockerfile.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Web apps
npm start fails because react-scripts is not found
react-scripts: command not found
npm runs scripts with node_modules/.bin on the PATH.
3 causes, each with a fixWeb apps
tsconfig rejects allowImportingTsExtensions
An error that allowImportingTsExtensions needs noEmit or emitDeclarationOnly
allowImportingTsExtensions lets files import each other with .ts or .tsx extensions, and TypeScript only allows it together with noEmit or emitDeclarationOnly.
2 causes, each with a fixWeb apps
Apollo Server rejects a request as a potential CSRF
HTTP 400 Bad Request from the GraphQL endpoint
Apollo Server's CSRF prevention is on by default.
3 causes, each with a fixWeb apps
React shows Attempted import error when importing a component or function
Attempted import error:
The build found the file but not the name you asked for.
2 causes, each with a fixWeb apps
AxiosError Network Error with code ERR_NETWORK in a React app
Network Error
ERR_NETWORK means the request produced no readable response.
4 causes, each with a fixWeb apps
Axios get works with a URL string but ignores the object passed as the second argument
The server log shows a GET with no query string
axios.get takes a URL and an optional config object.
3 causes, each with a fix