Introducing Livops 2.0! Promo codeCEOgives you 8,000 so‘m on your balance.What’s new

sh: vite: not found (also next, tsc) in npm run build: how to fix it

sh: 1: vite: not found, sh: next: not found or tsc: not found during npm run build (exit code 127) means the shell found no such command in node_modules/.bin. Usually the install skipped devDependencies because NODE_ENV=production or --omit=dev, the install never ran in that folder, or the tool was only installed globally on your laptop. Install it into the project and make the build's install include dev dependencies.

By Livops team, the people who build and run LivopsUpdated

As it appears in a build log (shortened; the shell’s wording varies between sh: 1: vite: not found and sh: vite: not found):

> [email protected] build
> tsc -b && vite build

sh: 1: tsc: not found

or, for Next.js:

> [email protected] build
> next build

sh: 1: next: not found

The build then stops with exit code 127. This page explains why a command that works on your machine is missing on the server, the four causes in the order they usually happen, the fix for each, and where it shows on Livops.

What does the error mean?

Two facts explain it:

  • The message comes from the shell, not from npm. npm runs a script by handing the line to a shell, /bin/sh on Unix-like systems[4]. When the shell cannot find a command, it fails with exit status 127 and writes an error message[12]. Current npm passes that exit code on as its own and adds nothing[7][8], which is why the log has no npm error lines in the simple case.
  • The shell looks in node_modules/.bin. npm run adds node_modules/.bin to the PATH it gives the script[4], so vite, next and tsc are found there without a path. Scripts run from the root of the package folder[4]. If that folder’s node_modules/.bin has no vite, the shell does not find it.

So vite: not found means one thing: when the build ran, node_modules/.bin/vite did not exist in that folder. The causes below are the ways that happens.

With npm workspaces, npm run build -w <workspace> prints its own block after the shell’s line, with the exit code in it[7]:

npm error Lifecycle script `build` failed with error:
npm error code 127
npm error workspace [email protected]

Causes, most common first

1. The install skipped devDependencies

The build tools are usually devDependencies. Vite’s own install command is npm install -D vite[9], and TypeScript’s is npm install typescript --save-dev[11]. npm describes devDependencies as tools that users of a package do not need, such as test or documentation frameworks[6], and a production install leaves them out:

  • with NODE_ENV=production, the omit setting defaults to dev[1], for npm install[2] and npm ci[3] alike;
  • --omit=dev does the same, and so does --production, its deprecated alias[1].

A platform, a Dockerfile or a CI file that sets NODE_ENV=production before the install therefore leaves vite and tsc out, and the build that follows cannot find them. Next.js is less often hit by this, because its manual install, npm i next@latest react@latest react-dom@latest, saves next to dependencies[10], but anything the build needs from devDependencies, typescript among them, is still missing.

Fix: keep the tools in devDependencies and install them for the build. Types listed in --include are installed whatever omit says, in whatever order the two are given[1]:

npm ci --include=dev
npm run build

Or set NODE_ENV=production only where the app runs, not where it is built. Note that when dev is omitted, npm also sets NODE_ENV=production for all lifecycle scripts[1]. Moving the build tools to dependencies works too, at the cost of installing them wherever the app runs.

2. node_modules is missing because the install did not run there

The build command alone does not install anything. If a CI step or a custom pipeline runs npm run build on a fresh checkout without an install before it, or runs the install in a different folder, there is no node_modules/.bin in the folder the script runs in. npm ci also deletes an existing node_modules before it installs[3], so a cached folder does not carry over either.

Fix: run the install in the same folder as the build, immediately before it:

npm ci            # or npm install when there is no lockfile
npm run build

If the install itself fails, its error comes first and is the one to fix; see npm ci: package.json and package-lock.json are not in sync.

3. The tool is not in package.json at all

tsc runs on your laptop because you once ran npm install -g typescript. A global install goes into npm’s prefix folder, not into the project[2], so the project’s package.json never mentions it and no server ever installs it. TypeScript’s own docs recommend a project install over a global one, so that builds are the same on every machine[11].

Fix: add the tool to the project and commit both files:

npm install -D vite                     # Vite
npm install typescript --save-dev       # tsc
npm i next@latest react@latest react-dom@latest   # Next.js
git add package.json package-lock.json

Then check that the scripts call the tools by name, as Vite’s scaffolded project and Next.js’s installation guide do: "build": "vite build"[9], "build": "next build"[10].

4. The wrong folder in a monorepo

In a repository with apps/web/package.json and a root package.json, the build may run in one folder while the tool is listed and installed in the other. With npm workspaces, the install runs once from the root[5], and a script in one workspace is run from the root by naming it[5]:

npm install vite -D -w apps/web   # list the tool in that workspace
npm ci                            # from the repository root
npm run build -w apps/web

Without workspaces, each folder with a package.json needs its own install, run in that folder.

How to confirm the fix locally

Reproduce the server: a clean tree, the same install, then the build.

rm -rf node_modules
NODE_ENV=production npm ci        # what a production install leaves out
ls node_modules/.bin | grep -E '^(vite|next|tsc)$'
npm ci --include=dev && npm run build

If the ls line prints nothing after a production install and the last line passes, cause 1 is yours. npm ls vite shows whether the project lists the tool at all.

On Livops

  • Where it shows. The build command ends with an error, the deploy stops as build_failed, and the previous version keeps running. The cabinet shows the code and the most useful line of the log; next to the failed build, the assistant’s button reads the log and explains what happened. See Build failed.

  • Check the install above the build. Livops detects the framework, Vite and Next.js among them, and from it the install and build commands. The install runs before the build and is in the same log. With a committed package-lock.json, the log says Found a npm lockfile — installing exactly those versions and runs npm ci.

  • Set the commands yourself. Change them in Settings → Install and Settings → Build, or in livops.json at the root of the repository or the project folder. install and build are applied on every deploy, and while the file names a field, the file wins over the cabinet:

    {
      "install": "npm ci --include=dev",
      "build": "npm run build"
    }
  • Monorepos. When you create the project from GitHub, set the folder the project lives in, for example apps/web. See Ways to deploy and Frameworks and livops.json.

  • Not vite alone as the build. A build command that starts a dev server (npm run dev, vite) never ends and runs out of time; use npm run build.

When the tool is found but a package it imports is not, see Error: Cannot find module; for other failures that end in code 1, see npm error code 1.

Questions

What does exit code 127 mean?

The shell could not find the command. POSIX shells exit with 127 when a command is not found, and npm run passes the script's exit code on as its own. So npm run build exiting with 127 means the first word of the build script, such as vite, next or tsc, was not on the PATH.

Why does npm run build work on my laptop but not on the server?

Your laptop has a node_modules folder with devDependencies in it, or the tool installed globally. The server starts from a clean checkout, so it has only what its own install put there. If that install ran with NODE_ENV=production or --omit=dev, vite and typescript were left out.

Should vite and typescript be in dependencies or devDependencies?

devDependencies is where their own docs put them: npm install -D vite and npm install typescript --save-dev. That is fine as long as the install that runs before the build includes dev dependencies. Add --include=dev to it if anything sets NODE_ENV=production.

Why does NODE_ENV=production break the build?

With NODE_ENV=production, npm's omit setting defaults to dev, so npm install and npm ci skip devDependencies. The build tools are usually devDependencies, so their commands never reach node_modules/.bin. Install with --include=dev, or set NODE_ENV=production only for the running app.

next is in my dependencies, so why is next not found?

Then the install did not run in the folder the build runs in. Check that node_modules/.bin/next exists after the install, that the install and the build run in the same folder, and in a monorepo that the package.json listing next is the one that was installed.

How do I fix it in a monorepo?

List the tool in the package.json of the workspace that uses it, install from the repository root so every workspace gets its dependencies, and run the script there with npm run build -w followed by the workspace folder, for example apps/web.

Sources

  1. npm Docs — config (omit, include) (docs.npmjs.com). Checked .
  2. npm Docs — npm install (docs.npmjs.com). Checked .
  3. npm Docs — npm ci (docs.npmjs.com). Checked .
  4. npm Docs — npm run (docs.npmjs.com). Checked .
  5. npm Docs — workspaces (docs.npmjs.com). Checked .
  6. npm Docs — package.json (docs.npmjs.com). Checked .
  7. npm source — lib/commands/run.js (github.com). Checked .
  8. npm source — lib/utils/error-message.js (github.com). Checked .
  9. Vite — Getting Started (vite.dev). Checked .
  10. Next.js Docs — Installation (nextjs.org). Checked .
  11. TypeScript — Download (typescriptlang.org). Checked .
  12. POSIX.1-2024 — Shell Command Language (pubs.opengroup.org). Checked .

Put your project online today

Connect GitHub or drop a folder. No card is asked for.

Deploy your Node app

Got a question? — @livops_uz