简体   繁体   中英

How to install Bootstrap 5 on Laravel 9 with Vite?

I followed the following guide:

https://getbootstrap.com/docs/5.2/getting-started/vite/

and I still get all sorts of errors, sometimes file is not found or sometimes I just don't see the bootstrap design.

I installed Bootstrap using npm:

npm install bootstrap@5.2.2

Then I installed scss using npm:

npm i --save-dev sass

Then I added the following to vite.config.js :

resolve: {
    alias: {
        '~bootstrap': path.resolve(__dirname, 'nodes_modules/bootstrap'),
    }
}

Also added the following to /resources/app.js :

import '/resources/scss/styles.scss'    
import * as bootstrap from 'bootstrap';

I also created a new scss folder in /resources and placed the following styles.scss file inside:

// Import all of Bootstrap's CSS
@import "~bootstrap/scss/bootstrap";

But when I use npm run dev or build , the Bootstrap styling don't show up, or I'm getting error about files not existing, for example:

[plugin:vite:css] [sass] ENOENT: no such file or directory, open '/var/www/myapp/nodes_modules/bootstrap/scss/bootstrap

Install Bootstrap 5 in Laravel 9 with Vite

  1. Install Bootstrap 5 and dependencies To install Bootstrap from the command terminal execute the following instructions
npm i bootstrap sass @popperjs/core --save-dev
  1. Configure Vite and dependencies Rename the file: resources/css/app.css to: resources/css/app.scss Open the vite.config.js file found in the root of the project and change the reference resources/css/app.css to resources/css/app.scss
import { defineConfig } from 'vite';
import laravel from 'laravel-vite-plugin';

export default defineConfig({
    plugins: [
        laravel({
            input: ['resources/css/app.scss', 'resources/js/app.js'],
            refresh: true,
        }),
    ],
});

In the resources/css/app.scss file import Bootstrap by adding the following line of code

@import 'bootstrap/scss/bootstrap';

In the file resources/js/bootstrap.js add the following lines

...
import * as Popper from '@popperjs/core'
window.Popper = Popper
import 'bootstrap'
...
  1. Change.blade for Hot reloading

In the resources/views/welcome.blade.php file, paste the following code before the closing tag:

...
   @vite(['resources/js/app.js', 'resources/css/app.scss'])
</head>
<body>
...
  1. Test Run dev server with command:
npm run dev

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM