38

I have a npm project that uses jquery.

var $ = require('jquery');

I also have an index html file that references bootstrap.

<link href="//netdna.bootstrapcdn.com/bootstrap/3.1.0/css/bootstrap.min.css" rel="stylesheet"/>

<script type="text/javascript" src="my-script.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>

<script src="//netdna.bootstrapcdn.com/bootstrap/3.1.0/js/bootstrap.min.js"></script>

Now do I need to a separate load for jquery for bootstrap as it depends on it? I was recently using jqueryify instead of jquery and I did not need the separate load.

Can anyone recommend a loading pattern for bootstrap with use with require?

1

8 Answers 8

61

Bootstrap is now a supported node package by the bootstrap guys.

https://www.npmjs.org/package/bootstrap

What this means is you can now require bootstrap in code. So.

npm install bootstrap --save

npm install jquery --save

foo.js

var $ = require('jquery');
window.$ = $;
require('bootstrap');
Sign up to request clarification or add additional context in comments.

5 Comments

What about CSS? Do we have to @import in our SASS files to the node_modules folder? Seems dirty.
@MichaelGiovanniPumo yes you would need to. I disagree that it is dirty.
@MichaelGiovanniPumo I want to expand on my comment. You don't necessarily need to. You just need to make sure that the CSS file is loaded. Could just be a <link> tag in your .html file. Or you could use sass/less/postcss to import in the file. I don't use bootstrap but in my postcss file i can call @import 'normalize.css' just like I would require modules in javascript. To me, that is clean.
Note that if you're using the bootstrap-sass npm module instead of regular bootstrap, the require('bootstrap') line would need to be require('bootstrap-sass').
This was helpful, but I also needed an alias in my Laravel Mix Webpack config file: stackoverflow.com/a/49576317/470749
15

If you have some trouble to use require('jquery'), there is a more simple way to do it. Just redirect node modules folders (without using any require()).

app.use('/js', express.static(__dirname + '/node_modules/bootstrap/dist/js')); // redirect bootstrap JS
app.use('/js', express.static(__dirname + '/node_modules/jquery/dist')); // redirect JS jQuery
app.use('/css', express.static(__dirname + '/node_modules/bootstrap/dist/css')); // redirect CSS bootstrap

And on your views, just use simply :

<link href="/css/bootstrap.min.css" rel="stylesheet">
<script src="/js/jquery.js"></script>
<script src="/js/bootstrap.min.js"></script>

Comments

13

Assuming you want to install the latest version of Bootstrap 4, and that you are using node 10+ which uses npm at least 5.6+ (I used it for node v12.14.1 and npm 6.13.6, and it works properly):

npm install bootstrap
npm install jquery
npm install popper.js

Notice that you don't need to pass --save since after npm 5.0.0 installed modules are added as a dependency by default.

Now, assuming you have a file called index.html at the same directory of node_modules, you need to add the following to your head tag:

<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta http-equiv="x-ua-compatible" content="ie=edge">

<!-- Bootstrap CSS -->
<link rel="stylesheet" href="node_modules/bootstrap/dist/css/bootstrap.min.css">

You also need to add the following before </body>:

<!-- jQuery first, then Popper.js, then Bootstrap JS. -->
<script src="node_modules/jquery/dist/jquery.slim.min.js"></script>
<script src="node_modules/popper.js/dist/umd/popper.min.js"></script>
<script src="node_modules/bootstrap/dist/js/bootstrap.min.js"></script>

Notice the comment: jQuery first, then Popper.js, then Bootstrap JS. This is important since Bootstrap depends upon JQuery and Popper.js.

Comments

2

You can try assigning it to global jquery property.

global.jQuery = require('jquery');

Comments

1
//install expose-loader
npm install jquery --save  //Installing jquery and saving to package.Json
npm install bootstrap --save//Installing boostrap and saving to package.Json
npm install expose-loader --save-dev//Installing expose-loader and saving to package.json

// webpack
module: {
rules: [
{
     test: require.resolve('jquery'),
     loader: 'expose-loader?jQuery!expose-loader?$'
}
]}

// javascript file 
var $ = require("expose-loader?$!jquery"); 
require('bootstrap');
require('jquery.easing');

1 Comment

please add a description with your code what it is doing and why should the OP use it, it would prevent your answer being flagged as low quality.
0
yarn add bootstrap-sass
yarn add jquery

For style, I use gulp sass which need to include Paths for bootstrap

@import "bootstrap-sprockets";
@import "bootstrap";

var sass = require('gulp-sass');

pipe(sass({includePaths: ['node_modules/bootstrap-sass/assets/stylesheets']}).on('error', sass.logError))

For JavaScript, I assign jquery to window.jQuery as bootstrap-sass needed:

window.jQuery = require('jquery');
require('bootstrap-sass');

Comments

0

I'm using Laravel Mix (Webpack), so I had to add this alias to webpack.mix.js:

mix.webpackConfig({
    ...
    resolve: {    
        ...
        alias: {        
            "bootstrap": path.resolve(__dirname, "node_modules/bootstrap/dist/js/bootstrap.min.js"),
        }
    },
});

My package.json has "bootstrap": "^4.0.0", within devDependencies.

Then in my javascript file for my view, I use:

var $ = require('jquery');
window.$ = $;
require('bootstrap');

https://getbootstrap.com/docs/4.0/getting-started/download/#npm also helped.

Comments

-3

Add this link below into head

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"/>

Add these links at the end body tag

<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" ></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js"></script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.