Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Installing Bootstrap with NPM

I know how to use npm to install and maintain build tools, but I realize I'm not sure how to leverage it to install frameworks (css or js I'd normally just slap the script/css links in my index.html).

But, let's say I want to use npm. I'd run an npm install bootstrap and npm install jquery, but then I'm not sure what the next step is.

I could then just say <link rel="stylesheet" type="text/css" href="nodemodules/boostrap/dist/css/bootstrap.css"> but that seems clunky and dumb.

I see a lot of people importing stuff like this with require('bootstrap'); but honestly I'm not even sure where to put that.

Could somebody help me fill in these blanks?

like image 797
Scott Ledbetter Avatar asked Jun 26 '18 23:06

Scott Ledbetter


People also ask

Can you install Bootstrap with NPM?

Install with npmrequire('bootstrap') will load all of Bootstrap's jQuery plugins onto the jQuery object. The bootstrap module itself does not export anything. You can manually load Bootstrap's jQuery plugins individually by loading the /js/*.js files under the package's top-level directory.

What is NPM run Bootstrap?

Bootstrap uses NPM scripts for its build system. Our package. json includes convenient methods for working with the framework, including compiling code, running tests, and more. To use our build system and run our documentation locally, you'll need a copy of Bootstrap's source files and Node.

How do I download and install Bootstrap?

This wikiHow teaches you how to download the Bootstrap files to your computer, and link them to your HTML texts to use Bootstrap elements in your code. Once you download and link the Bootstrap files, you can start using all the stylesheet and JavaScript elements of Bootstrap in your web design.


2 Answers

A common way to handle this is via grunt tasks, which copy the relevant files out of node_modules, into a more appropriate folder.

This is an excerpt from a copy task which I used for a recent project...

    copy: {
        types: {
            files: [
                {
                    expand: true,
                    cwd: 'node_modules/@types',
                    src: ['**/*.ts'],
                    dest: 'scripts/typings'
                }
            ]
        },
        jquery: {
            files: [
                {
                    expand: true,
                    cwd: 'node_modules/jquery/dist',
                    src: ['*.js'],
                    dest: 'Content/libraries/jquery'
                }
            ]
        },
        handlebars: {
            files: [
                {
                    expand: true,
                    cwd: 'node_modules/handlebars/dist',
                    src: ['*.js'],
                    dest: 'Content/libraries/handlebars'
                }
            ]
        },
        bootstrap: {
            files: [
                {
                    expand: true,
                    cwd: 'node_modules/bootstrap',
                    src: ['dist/js/*.js', 'dist/fonts/*.*', 'dist/css/*.css'],
                    dest: 'Content/libraries/bootstrap'
                }
            ]
        }
    }

There may be more succinct ways to write this, but it works for me.

This allows me to update my website packages by doing the following...

$ npm install
$ grunt copy

The folders /Content/libraries, and /scripts/typings are committed to my application git repo, node_modules isn't. This helps prevent issues if a module become unavailable at some stage in the future. My git repo contains everything required for the site to function.

If for some reason one of these modules was removed from npm, it would just mean I couldn't do upgrades, but my site will still work with the files that my git repo includes.

In my case, I don't actually need node_modules at all in my runtime environment, as I'm not using nodejs. I'm just using npm to manage my application's client-side module dependencies.

like image 77
user1751825 Avatar answered Oct 18 '22 01:10

user1751825


This depends on your build configuration but for the sake of this answer let's say you have an app.js file where you import the rest of your styles and scripts and you have a webpack configuration that processes this file. Then you only need to install the following dependencies:

npm i popper.js jquery bootstrap

(popper.js is a bootstrap dependency)

Then you just have to import them in your app.js file like so:

app.js

  window.Popper = require("popper.js").default;
  window.$ = window.jQuery = require("jquery");

  require("bootstrap");

You also need to import the styles of bootstrap of course. You could do that in several ways. Let's say you have an app.scss file for your styles. Then you can do it like so:

app.scss

@import "~bootstrap/scss/bootstrap";
like image 1
jogarcia Avatar answered Oct 18 '22 01:10

jogarcia