How to install Laravel Vite?

Viewed 2182

The Laravel Vite Doc sais to run:

npx apply laravel:vite --ignore-existing

inside your project root to install vite in your laravel project.

When I try to do that in a freshly installed laravel project it shows this:

zsh:1: command not found: laravel:vite

What am I doing wrong?

Using Macos Big Sur with PhpStorm.

2 Answers

That command no longer applies to newly created projects (as of laravel-vite 0.1.27, released a few days ago), and it now results in a different error:

$ npx apply laravel:vite --ignore-existing
[ info ]  Applying preset laravel:vite.
[ error ]  The preset could not be evaluated.
evalmachine.<anonymous>:13
var preset_default = definePreset({
                     ^

ReferenceError: definePreset is not defined
    at evalmachine.<anonymous>:13:22
    at Script.runInContext (node:vm:139:12)
    at Object.runInContext (node:vm:289:6)
    at ModuleImporter.evaluateConfiguration (/Users/tony/src/laravel-vite-demo/example-app/node_modules/apply/dist/Importer/ModuleImporter.js:68:26)
    at ModuleImporter.import (/Users/tony/src/laravel-vite-demo/example-app/node_modules/apply/dist/Importer/ModuleImporter.js:17:27)
    at PresetApplier.run (/Users/tony/src/laravel-vite-demo/example-app/node_modules/apply/dist/Applier/PresetApplier.js:22:87)
    at async CommandLineInterface.apply (/Users/tony/src/laravel-vite-demo/example-app/node_modules/apply/dist/IO/CommandLineInterface.js:57:16)
    at async CommandLineInterface.run (/Users/tony/src/laravel-vite-demo/example-app/node_modules/apply/dist/IO/CommandLineInterface.js:54:16)

However, the command shown in the laravel-presets/vite repository (the source repo for the laravel:vite preset) worked for me -- i.e., it augmented the Laravel project with Vite:

# Run this command from root of Laravel project
npx @preset/cli apply --debug laravel:vite 

@preset/cli currently suppresses all output (including errors), so the --debug flag above is important. Since the verbose log is difficult to follow, I recommend redirecting the output to a file, and searching it for the word exception.

Tips

  • To ensure the preset succeeds, you must have php@8.0 (e.g., from Homebrew) and composer installed and available from the command line. Add the paths to those binaries to your PATH environment variable.

  • The Vite server must be started separately from the PHP server. Run npm run dev in one terminal; and php artisan serve from another.

Related