Reputation: 113
I'm very new to svelte ( like many of us ^^ ), and I didn't manage to add bootstrap to my app. Tried to run 'npm add bootstrap' but it said that I need peer jquery dependencie. Here is the terminal render
What I don't understand is why the package has been added and I can't still use the bootstrap classes. Second point, why does it talk about peer dependencies? What's the link here? I don't know if I'm missing something but if you guys got the solution it will help a lot. Thank you
npm add bootstrap
npm WARN [email protected] requires a peer of [email protected] - 3 but none is installed. You must install peer dependencies yourself.
npm WARN [email protected] No repository field.
npm WARN [email protected] No license field.
+ [email protected]
added 1 package from 2 contributors and audited 9125 packages in 8.047s
found 0 vulnerabilities```
Upvotes: 10
Views: 21217
Reputation: 21
In Sveltekite, steps need to be followed:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="icon" href="%sveltekit.assets%/favicon.png" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<script src="%sveltekit.assets%/dist/js/bootstrap.bundle.min.js"></script>
%sveltekit.head%
</head>
<body data-sveltekit-preload-data="hover">
<div style="display: contents">%sveltekit.body%</div>
</body>
</html>
All steps in this single screenshot.
Upvotes: -1
Reputation: 15907
This is what I did:
import { onMount } from 'svelte';
onMount(async () => {
await import('bootstrap/dist/css/bootstrap.css')
window.bootstrap = await import('bootstrap/dist/js/bootstrap.esm.js')
// Some code that uses bootstrep - e.g.:
const tooltipTriggerList = document.querySelectorAll('[data-bs-toggle="tooltip"]')
const tooltipList = [...tooltipTriggerList].map(tooltipTriggerEl => new bootstrap.Tooltip(tooltipTriggerEl))
})
Upvotes: -1
Reputation: 165
I successfully added Bootstrap 5 to my Svelte project, here is how to do this:
npm install
or yarn
(if you prefer yarn) to download dependencies listed in package.json
.npm install bootstrap
(or npm i bootstrap
) or yarn add bootstrap
. You can specify version if you want. Its folder will appear among other dependencies in node_modules
.popper.js
and jQuery
we also need to add them:
npm i @popperjs/core jquery
.node_modules
folder) we need to link it to our application. Since Svelte app is basically an html page we can simply add links to Bootstrap CSS and JS in src/app.html:<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="icon" href="%sveltekit.assets%/favicon.png" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="stylesheet" href="../node_modules/bootstrap/dist/css/bootstrap.min.css">
%sveltekit.head%
</head>
<body>
<div>%sveltekit.body%</div>
<script src="../node_modules/bootstrap/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>
Using aliases for node_modules
is not preconfigured in vite.config.js
by default.
That's it, this worked for me.
Upvotes: -1
Reputation: 1962
There is also sveltestrap as one of the Svelte Society design systems
Upvotes: 1
Reputation: 51
This answer is an addition to the accepted answer's second option. (I cannot comment yet..)
Instead of copying all of Bootstrap's files to the public
folder, you can also pick and choose. For example, I only needed the minified CSS and the bundled minified JavaScript, so I configured the copy
plugin like this:
//...
import copy from "rollup-plugin-copy";
export default {
//...
plugins: [
//...
copy({
targets: [
{
src: "node_modules/bootstrap/dist/css/bootstrap.min.css",
dest: "public/vendor/bootstrap/css",
},
{
src: "node_modules/bootstrap/dist/js/bootstrap.bundle.min.js",
dest: "public/vendor/bootstrap/js",
},
],
}),
//...
],
//...
};
The CSS can be included in the <head>
tag in public/index.html
:
<link rel='stylesheet' href='vendor/bootstrap/css/bootstrap.min.css'>
The JavaScript can be included inside of the <body>
tag (at the end) in public/index.html
:
<script src="vendor/bootstrap/js/bootstrap.bundle.min.js"></script>
Upvotes: 4
Reputation: 3186
Your problem is that installing bootstrap
into node_modules doesn't automatically include the files in your application. There are a few different ways you can accomplish this.
/public
manuallypublic
folder of your svelte project.public/index.html
. <link rel='stylesheet' href='bootstrap/dist/css/bootstrap.min.css'>
. The downside of this approach is that you will need to commit the bootstrap folder to your version control system, which is generally frowned upon.
/public
using rolluprollup.config.js
to include the copy
plugin. Here is a snippet of the important parts from a rollup.config.js file from a fresh svelte install.//...
import copy from 'rollup-plugin-copy'
export default {
//...
plugins: [
//...
copy({
targets: [{
src: 'node_modules/bootstrap/dist/**/*',
dest: 'public/vendor/bootstrap'
}]
}),
//...
],
//...
};
public/index.html
. <link rel='stylesheet' href='vendor/bootstrap/css/bootstrap.min.css'>
Upvotes: 27