Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add bootstrap module in a svelte JS app?

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```
like image 808
Louis Chamaillard Avatar asked Jan 21 '20 13:01

Louis Chamaillard


People also ask

How do I add bootstrap to a svelte project?

Download the bootstrap files and place them inside the public folder of your svelte project. Add the css link in 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.

Why can't I install bootstrap in node_modules?

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. Download the bootstrap files and place them inside the public folder of your svelte project.

How do I use svelte components?

Then, once you've got your project set up, using Svelte components is easy. The compiler turns each component into a regular JavaScript class — just import it and instantiate with new: You can then interact with app using the component API if you need to. loading editor...

How do I add bootstrap CSS to my website?

Add the css link in 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.


2 Answers

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.

Option 1: Copy the files to /public manually

  1. Download the bootstrap files and place them inside the public folder of your svelte project.
  2. Add the css link in 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.

Option 2: Copy the files to /public using rollup

  1. install rollup-plugin-copy
  2. Update rollup.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' 
            }]
        }),
       //...
    ],
    //...
};
  1. Add the css link in public/index.html. <link rel='stylesheet' href='vendor/bootstrap/css/bootstrap.min.css'>
like image 52
TwitchBronBron Avatar answered Sep 21 '22 06:09

TwitchBronBron


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>
like image 45
Han Avatar answered Sep 19 '22 06:09

Han