dcranmer's avatar

npm run build error: sh: vite: command not found

I'm trying to deploy a refactored Laravel/Inertia/Vue3 app to our "staging" server (which is a test production environment), but can't seem to run vite build. The error I get is:

> postinstall
> npm run build
> build
> vite build && vite build --ssr && vite build && vite build --ssr --ssr
npm stderr: sh: vite: command not found

I see that vite (v4x) is installed in node_modules. The server (running Red Hat Enterprise something...) is running node v16.20. I can run npm run dev in my dev environment without issue. The previous version of the app ran Laravel Mix/Webpack. What can/should I check, change, look for to get this to work?

0 likes
6 replies
LaryAI's avatar
Level 58

The error message "sh: vite: command not found" indicates that the "vite" command is not recognized by the server. Here are a few steps you can take to resolve this issue:

  1. Verify that vite is installed:

    • Navigate to the root directory of your project.
    • Run the command npm ls vite to check if vite is listed as a dependency. If it is not listed, you may need to reinstall it by running npm install vite.
  2. Check the npm scripts in your package.json file:

    • Open your package.json file and locate the "scripts" section.
    • Ensure that the "build" script is defined correctly. It should include the command to run vite, such as "build": "vite build".
    • If the script is missing or incorrect, update it accordingly.
  3. Check the server's environment:

    • Verify that the server has Node.js and npm installed. You can check the versions by running node -v and npm -v respectively.
    • If Node.js and npm are not installed, you will need to install them on the server.
  4. Check the server's PATH variable:

    • It's possible that the server's PATH variable does not include the location of the vite executable.
    • You can try adding the vite executable path to the PATH variable by running the following command:
      export PATH=$PATH:/path/to/vite
      
      Replace "/path/to/vite" with the actual path to the vite executable.
  5. Try running vite directly:

    • If none of the above steps work, try running the vite command directly on the server to see if it is recognized.
    • SSH into the server and navigate to the root directory of your project.
    • Run the command npx vite build to see if it works. If it does, there may be an issue with the npm scripts or the server's configuration.

If the issue persists, it may be helpful to provide more information about your project's setup, such as the contents of your package.json file and any relevant configuration files.

1 like
dcranmer's avatar

Anyone else have an idea or suggestion, please? vite is present in npm_modules/.bin. I don't know how to check or change the path for whichever user tuns the npm commands.

On the other hand, I am able to run `npm run build`` on my local machine. So I'm not sure why the server can't find vite.

mdnurulmomen's avatar

For future searchers !

Steps I followed:

  1. Remove the node_modules folder,
  2. Install the dependency using npm install
  3. Run the server using php artisan serve
  4. Finally run the dev server using npm run dev

It should start the dev server and all your javascript code should be compiled.

1 like
marykline120's avatar

This error usually means Vite isn’t available in the server’s PATH during the build step, even though it exists in node_modules. On older Node versions—like 16.20—Vite 4 often fails because it expects a more recent runtime, so upgrading Node to at least 18 LTS is the first thing to confirm. Also verify that your deployment process is running npm ci or npm install without --production, since Vite is a devDependency and won’t be installed if production-only installs are used. Finally, ensure that the build script is executed from the project root so the local node_modules/.bin/vite is accessible. Once you sort out the Node version and install context, the build should run normally; and while you’re at it, you might also want to check out.... Try out this App: www.cinebyhd.app

Please or to participate in this conversation.