---
title: Bootstrap and npm
-description: The official guide for how to build a starter project with Bootstrap’s CSS and JavaScript in your project using just npm.
+description: The official guide for how to build a starter project with Bootstrap's CSS and JavaScript in your project using just npm.
toc: true
thumbnail: guides/bootstrap-npm@2x.png
---
-<img class="d-block mx-auto img-fluid rounded-3" srcset="/docs/[[config:docs_version]]/assets/img/guides/bootstrap-npm.png, /docs/[[config:docs_version]]/assets/img/guides/bootstrap-npm@2x.png 2x" src="/docs/[[config:docs_version]]/assets/img/guides/bootstrap-npm.png" width="1000" height="500" alt=""/>
+<img class="d-block mx-auto mb-4 img-fluid rounded-3" srcset="/docs/[[config:docs_version]]/assets/img/guides/bootstrap-npm.png, /docs/[[config:docs_version]]/assets/img/guides/bootstrap-npm@2x.png 2x" src="/docs/[[config:docs_version]]/assets/img/guides/bootstrap-npm.png" width="1000" height="500" alt=""/>
<Callout>
**Want to skip to the end?** Download the source code and working demo for this guide from the [twbs/examples repository](https://github.com/twbs/examples/tree/sass-js/). You can also [open the example in StackBlitz](https://stackblitz.com/github/twbs/examples/tree/main/sass-js?file=index.html).
</Callout>
+## What is npm?
+
+[npm](https://www.npmjs.com/) is the default package manager for Node.js. In this guide, we use npm to install Bootstrap and its dependencies, then compile Sass to CSS using command-line tools—no bundler required.
+
## Setup
-We’re building a npm project with Bootstrap from scratch, so there are some prerequisites and upfront steps before we get started. This guide requires you to have Node.js installed and some familiarity with the terminal.
+We're building a npm project with Bootstrap from scratch, so there are some prerequisites and upfront steps before we get started. This guide requires you to have Node.js installed and some familiarity with the terminal.
1. **Create a project folder and set up npm.** We'll create the `my-project` folder and initialize npm with the `-y` argument to avoid it asking us all the interactive questions.
npm init -y
```
-2. **Install Bootstrap.** Now we can install Bootstrap. We'll also install Floating UI since our dropdowns, popovers, and tooltips depend on it for their positioning. If you don’t plan on using those components, you can omit Floating UI here.
+2. **Install Bootstrap.** Now we can install Bootstrap. We'll also install Floating UI since our dropdowns, popovers, and tooltips depend on it for their positioning. If you don't plan on using those components, you can omit Floating UI here.
```sh
npm i --save bootstrap @floating-ui/dom
```
-3. **Install additional dependencies.** In addition to Vite and Bootstrap, we have some dependencies used to import and bundle Bootstrap’s CSS. Sass compiles our source `.scss` files into CSS and then Autoprefixer and PostCSS handle browser compatibility. We install the `postcss-cli` package so we can work with PostCSS via CLI.
+
+3. **Install additional dependencies.** In addition to Bootstrap, we need a few more dependencies to compile and post-process our CSS. Sass compiles our source `.scss` files into CSS, and Autoprefixer and PostCSS handle browser compatibility. We install the `postcss-cli` package so we can run PostCSS from the command line.
```sh
npm i --save-dev autoprefixer postcss postcss-cli sass
## Project structure
-We’ve already created the `my-project` folder and initialized npm. Now we'll also create our `src` folder, stylesheet, and JavaScript file to round out the project structure. Run the following from `my-project`, or manually create the folder and file structure shown below.
+We've already created the `my-project` folder and initialized npm. Now we'll also create our `scss` folder, stylesheet, and configuration files to round out the project structure. Run the following from `my-project`, or manually create the folder and file structure shown below.
```sh
-mkdir {css,js,scss}
-touch index.html js/main.js scss/styles.scss .stylelintrc.json
+mkdir {css,scss}
+touch index.html scss/styles.scss postcss.config.js .stylelintrc.json
```
-When you’re done, your project should look like this:
+When you're done, your project should look like this:
```text
my-project/
├── css/
-├── js/
-│ └── main.js
├── scss/
│ └── styles.scss
+├── .stylelintrc.json
├── index.html
├── package-lock.json
-└── package.json
+├── package.json
+└── postcss.config.js
```
-At this point, everything is in the right place, but we’ll need to add some npm scripts to use our dependencies and compile the CSS.
+At this point, everything is in the right place, but we'll need to configure our files and add some npm scripts to compile the CSS.
+
+## Configure npm
+
+With dependencies installed and our project folder ready for us to start coding, we can now configure our npm scripts and run our project locally.
+
+1. **Open `postcss.config.js` in your editor.** We need to configure Autoprefixer to run after Sass compilation for browser compatibility.
+
+ ```js
+ const autoprefixer = require('autoprefixer')
+
+ module.exports = {
+ plugins: [
+ autoprefixer
+ ]
+ }
+ ```
+
+2. **Fill in the `index.html` file.** We need an HTML page to render our project. This page links to the compiled CSS file and includes Bootstrap's pre-built JavaScript bundle.
+
+ ```html
+ <!doctype html>
+ <html lang="en">
+ <head>
+ <meta charset="utf-8">
+ <meta name="viewport" content="width=device-width, initial-scale=1">
+ <title>Bootstrap w/ npm</title>
+ <link rel="stylesheet" href="css/styles.css">
+ </head>
+ <body>
+ <div class="container py-4 px-3 mx-auto">
+ <h1>Hello, Bootstrap and npm!</h1>
+ <button class="btn-solid theme-primary">Primary button</button>
+ </div>
+ <script src="node_modules/bootstrap/dist/js/bootstrap.bundle.min.js"></script>
+ </body>
+ </html>
+ ```
+
+ We're including a little bit of Bootstrap styling here with the `div class="container"` and `<button>` so that we see when Bootstrap's CSS is loaded by our npm scripts. Since this setup doesn't use a JavaScript bundler, we load Bootstrap's pre-built JS bundle directly via a `<script>` tag.
+
+ *[Read our JavaScript docs]([[docsref:/getting-started/javascript/]]) for more information on how to use Bootstrap's plugins.*
+
+3. **Add npm scripts to `package.json`.** Open `package.json` and add the following scripts. We use `sass` to compile our Sass (with `--load-path=node_modules` so it can resolve Bootstrap), `postcss-cli` to apply Autoprefixer, `nodemon` to watch for changes, `npm-run-all` to run scripts in sequence or in parallel, `sirv-cli` to serve our project, and `stylelint` to lint our Sass.
+
+ ```json
+ {
+ // ...
+ "scripts": {
+ "css-compile": "sass --load-path=node_modules --style expanded --source-map --embed-sources scss:css",
+ "css-prefix": "postcss --config postcss.config.js --replace \"css/*.css\" \"!css/*.min.css\"",
+ "build": "npm-run-all --sequential css-compile css-prefix",
+ "watch": "nodemon --watch scss/ --ext scss --exec \"npm run build\"",
+ "serve": "sirv --port 8080 --dev .",
+ "start": "npm-run-all build --parallel watch serve",
+ "lint": "stylelint \"scss/**/*.scss\""
+ },
+ // ...
+ }
+ ```
+
+4. **Configure `.stylelintrc.json`.** We're using `stylelint-config-twbs-bootstrap` to keep our Sass linting consistent with Bootstrap's own code style.
+
+ ```json
+ {
+ "extends": "stylelint-config-twbs-bootstrap"
+ }
+ ```
+
+5. **And finally, we can start the npm scripts.** From the `my-project` folder in your terminal, run that newly added npm script:
+
+ ```sh
+ npm start
+ ```
-## Configure
+ {/* <img class="img-fluid" src="/docs/[[config:docs_version]]/assets/img/guides/npm-dev-server.png" alt="npm dev server running" /> */}
-- npm scripts
-- stylelint configuration
+In the next and final section to this guide, we'll import all of Bootstrap's CSS.
## Import Bootstrap
+Importing Bootstrap into our project requires adding a Sass import to our `scss/styles.scss` file.
+
+1. **Import Bootstrap's CSS.** Add the following to `scss/styles.scss` to import all of Bootstrap's source Sass. We use the `@use` rule, which is the modern Sass module system.
+
+ ```scss
+ // Import all of Bootstrap's CSS
+ @use "bootstrap/scss/bootstrap";
+ ```
+
+ *You can also import our stylesheets individually if you want. [Read our Sass import docs]([[docsref:/customize/sass#importing]]) for details.*
+
+2. **Optionally, customize Bootstrap's Sass tokens.** Since Bootstrap uses Sass modules (`@use`/`@forward`), you can override default values using the `with ()` syntax. You only need to include the keys you want to change — Bootstrap merges your overrides with its defaults.
+
+ ```scss
+ @use "bootstrap/scss/bootstrap" with (
+ $root-tokens: (
+ --border-radius: .25rem,
+ --spacer: 1.5rem,
+ ),
+ $alert-tokens: (
+ --alert-padding-x: 2rem,
+ --alert-border-radius: 1rem,
+ )
+ );
+ ```
+
+ *[Read our Sass customization docs]([[docsref:/customize/sass#token-defaults]]) for the full list of available token maps and options.*
+
+3. **And you're done! 🎉** With Bootstrap's source Sass fully loaded, your local development server should now look like this:
+
+ {/* <img class="img-fluid" src="/docs/[[config:docs_version]]/assets/img/guides/npm-dev-server-bootstrap.png" alt="npm dev server running with Bootstrap" /> */}
+
+ Now you can start adding any Bootstrap components you want to use. Be sure to [check out the complete npm Sass and JS example project](https://github.com/twbs/examples/tree/main/sass-js) for how to include additional custom Sass and optimize your build by importing only the parts of Bootstrap's CSS and JS that you need.
+
<GuideFooter />
---
title: Bootstrap and Parcel
-description: The official guide for how to include and bundle Bootstrap’s CSS and JavaScript in your project using Parcel.
+description: The official guide for how to include and bundle Bootstrap's CSS and JavaScript in your project using Parcel.
toc: true
thumbnail: guides/bootstrap-parcel@2x.png
---
<img class="d-block mx-auto mb-4 img-fluid rounded-3" srcset="/docs/[[config:docs_version]]/assets/img/guides/bootstrap-parcel.png, /docs/[[config:docs_version]]/assets/img/guides/bootstrap-parcel@2x.png 2x" src="/docs/[[config:docs_version]]/assets/img/guides/bootstrap-parcel.png" width="1000" height="500" alt=""/>
<Callout>
-**Want to skip to the end?** Download the source code and working demo for this guide from the [twbs/examples repository](https://github.com/twbs/examples/tree/main/parcel). You can also [open the example in StackBlitz](https://stackblitz.com/github/twbs/examples/tree/main/parcel?file=index.html) but not run it because Parcel isn’t currently supported there.
+**Want to skip to the end?** Download the source code and working demo for this guide from the [twbs/examples repository](https://github.com/twbs/examples/tree/main/parcel). You can also [open the example in StackBlitz](https://stackblitz.com/github/twbs/examples/tree/main/parcel?file=index.html) but not run it because Parcel isn't currently supported there.
</Callout>
## What is Parcel?
## Setup
-We’re building a Parcel project with Bootstrap from scratch, so there are some prerequisites and upfront steps before we can really get started. This guide requires you to have Node.js installed and some familiarity with the terminal.
+We're building a Parcel project with Bootstrap from scratch, so there are some prerequisites and upfront steps before we can really get started. This guide requires you to have Node.js installed and some familiarity with the terminal.
1. **Create a project folder and set up npm.** We'll create the `my-project` folder and initialize npm with the `-y` argument to avoid it asking us all the interactive questions.
npm init -y
```
-2. **Install Parcel.** Unlike our Webpack guide, there’s only a single build tool dependency here. Parcel will automatically install language transformers (like Sass) as it detects them. We use `--save-dev` to signal that this dependency is only for development use and not for production.
+2. **Install Parcel.** Unlike our Webpack guide, there's only a single build tool dependency here. Parcel will automatically install language transformers (like Sass) as it detects them. We use `--save-dev` to signal that this dependency is only for development use and not for production.
```sh
npm i --save-dev parcel
```
-3. **Install Bootstrap.** Now we can install Bootstrap. We'll also install Floating UI since our dropdowns, popovers, and tooltips depend on it for their positioning. If you don’t plan on using those components, you can omit Floating UI here.
+3. **Install Bootstrap.** Now we can install Bootstrap. We'll also install Floating UI since our dropdowns, popovers, and tooltips depend on it for their positioning. If you don't plan on using those components, you can omit Floating UI here.
```sh
npm i --save bootstrap @floating-ui/dom
## Project structure
-We’ve already created the `my-project` folder and initialized npm. Now we'll also create our `src` folder, stylesheet, and JavaScript file to round out the project structure. Run the following from `my-project`, or manually create the folder and file structure shown below.
+We've already created the `my-project` folder and initialized npm. Now we'll also create our `src` folder, stylesheet, and JavaScript file to round out the project structure. Run the following from `my-project`, or manually create the folder and file structure shown below.
```sh
mkdir {src,src/js,src/scss}
touch src/index.html src/js/main.js src/scss/styles.scss
```
-When you’re done, your complete project should look like this:
+When you're done, your complete project should look like this:
```text
my-project/
</html>
```
- We’re including a little bit of Bootstrap styling here with the `div class="container"` and `<button>` so that we see when Bootstrap’s CSS is loaded by Parcel.
+ We're including a little bit of Bootstrap styling here with the `div class="container"` and `<button>` so that we see when Bootstrap's CSS is loaded by Parcel.
- Parcel will automatically detect we’re using Sass and install the [Sass Parcel plugin](https://parceljs.org/languages/sass/) to support it. However, if you wish, you can also manually run `npm i --save-dev @parcel/transformer-sass`.
+ Parcel will automatically detect we're using Sass and install the [Sass Parcel plugin](https://parceljs.org/languages/sass/) to support it. However, if you wish, you can also manually run `npm i --save-dev @parcel/transformer-sass`.
-2. **Add the Parcel npm scripts.** Open the `package.json` and add the following `start` script to the `scripts` object. We'll use this script to start our Parcel development server and render the HTML file we created after it’s compiled into the `dist` directory.
+2. **Add the Parcel npm scripts.** Open the `package.json` and add the following `start` script to the `scripts` object. We'll use this script to start our Parcel development server and render the HTML file we created after it's compiled into the `dist` directory.
```json
{
<img class="img-fluid" src="/docs/[[config:docs_version]]/assets/img/guides/parcel-dev-server.png" alt="Parcel dev server running" />
-In the next and final section to this guide, we'll import all of Bootstrap’s CSS and JavaScript.
+In the next and final section to this guide, we'll import all of Bootstrap's CSS and JavaScript.
## Import Bootstrap
Importing Bootstrap into Parcel requires two imports, one into our `styles.scss` and one into our `main.js`.
-1. **Import Bootstrap’s CSS.** Add the following to `src/scss/styles.scss` to import all of Bootstrap’s source Sass.
+1. **Import Bootstrap's CSS.** Add the following to `src/scss/styles.scss` to import all of Bootstrap's source Sass.
```scss
- // Import all of Bootstrap’s CSS
- @import "bootstrap/scss/bootstrap";
+ // Import all of Bootstrap's CSS
+ @use "bootstrap/scss/bootstrap";
```
*You can also import our stylesheets individually if you want. [Read our Sass import docs]([[docsref:/customize/sass#importing]]) for details.*
- **Optional:** You may see Sass deprecation warnings with the latest versions of Dart Sass. These can silenced by adding the following configuration in a `.sassrc.js` file in the root folder with the following:
+2. **Import Bootstrap's JS.** Add the following to `src/js/main.js` to import all of Bootstrap's JS. Floating UI will be imported automatically through Bootstrap.
```js
- module.exports = {
- silenceDeprecations: ['import', 'mixed-decls', 'color-functions', 'global-builtin']
- }
- ```
-
-2. **Import Bootstrap’s JS.** Add the following to `src/js/main.js` to import all of Bootstrap’s JS. Floating UI will be imported automatically through Bootstrap.
-
- ```js
- // Import all of Bootstrap’s JS
+ // Import all of Bootstrap's JS
import * as bootstrap from 'bootstrap'
```
import { Tooltip, Toast, Popover } from 'bootstrap'
```
- *[Read our JavaScript docs]([[docsref:/getting-started/javascript/]]) for more information on how to use Bootstrap’s plugins.*
+ *[Read our JavaScript docs]([[docsref:/getting-started/javascript/]]) for more information on how to use Bootstrap's plugins.*
-3. **And you’re done! 🎉** With Bootstrap’s source Sass and JS fully loaded, your local development server should now look like this:
+3. **And you're done! 🎉** With Bootstrap's source Sass and JS fully loaded, your local development server should now look like this:
<img class="img-fluid" src="/docs/[[config:docs_version]]/assets/img/guides/parcel-dev-server-bootstrap.png" alt="Parcel dev server running with Bootstrap" />
- Now you can start adding any Bootstrap components you want to use. Be sure to [check out the complete Parcel example project](https://github.com/twbs/examples/tree/main/parcel) for how to include additional custom Sass and optimize your build by importing only the parts of Bootstrap’s CSS and JS that you need.
+ Now you can start adding any Bootstrap components you want to use. Be sure to [check out the complete Parcel example project](https://github.com/twbs/examples/tree/main/parcel) for how to include additional custom Sass and optimize your build by importing only the parts of Bootstrap's CSS and JS that you need.
<GuideFooter />
---
title: Bootstrap and Vite
-description: The official guide for how to include and bundle Bootstrap’s CSS and JavaScript in your project using Vite.
+description: The official guide for how to include and bundle Bootstrap's CSS and JavaScript in your project using Vite.
toc: true
thumbnail: guides/bootstrap-vite@2x.png
---
## Setup
-We’re building a Vite project with Bootstrap from scratch, so there are some prerequisites and upfront steps before we can really get started. This guide requires you to have Node.js installed and some familiarity with the terminal.
+We're building a Vite project with Bootstrap from scratch, so there are some prerequisites and upfront steps before we can really get started. This guide requires you to have Node.js installed and some familiarity with the terminal.
1. **Create a project folder and set up npm.** We'll create the `my-project` folder and initialize npm with the `-y` argument to avoid it asking us all the interactive questions.
npm init -y
```
-2. **Install Vite.** Unlike our Webpack guide, there’s only a single build tool dependency here. We use `--save-dev` to signal that this dependency is only for development use and not for production.
+2. **Install Vite.** Unlike our Webpack guide, there's only a single build tool dependency here. We use `--save-dev` to signal that this dependency is only for development use and not for production.
```sh
npm i --save-dev vite
```
-3. **Install Bootstrap.** Now we can install Bootstrap. We'll also install Floating UI since our dropdowns, popovers, and tooltips depend on it for their positioning. If you don’t plan on using those components, you can omit Floating UI here.
+3. **Install Bootstrap.** Now we can install Bootstrap. We'll also install Floating UI since our dropdowns, popovers, and tooltips depend on it for their positioning. If you don't plan on using those components, you can omit Floating UI here.
```sh
npm i --save bootstrap @floating-ui/dom
```
-4. **Install additional dependency.** In addition to Vite and Bootstrap, we need another dependency (Sass) to properly import and bundle Bootstrap’s CSS.
+4. **Install additional dependency.** In addition to Vite and Bootstrap, we need another dependency (Sass) to properly import and bundle Bootstrap's CSS.
```sh
npm i --save-dev sass
## Project structure
-We’ve already created the `my-project` folder and initialized npm. Now we'll also create our `src` folder, stylesheet, and JavaScript file to round out the project structure. Run the following from `my-project`, or manually create the folder and file structure shown below.
+We've already created the `my-project` folder and initialized npm. Now we'll also create our `src` folder, stylesheet, and JavaScript file to round out the project structure. Run the following from `my-project`, or manually create the folder and file structure shown below.
```sh
mkdir {src,src/js,src/scss}
touch src/index.html src/js/main.js src/scss/styles.scss vite.config.js
```
-When you’re done, your complete project should look like this:
+When you're done, your complete project should look like this:
```text
my-project/
└── vite.config.js
```
-At this point, everything is in the right place, but Vite won’t work because we haven’t filled in our `vite.config.js` yet.
+At this point, everything is in the right place, but Vite won't work because we haven't filled in our `vite.config.js` yet.
## Configure Vite
With dependencies installed and our project folder ready for us to start coding, we can now configure Vite and run our project locally.
-1. **Open `vite.config.js` in your editor.** Since it’s blank, we'll need to add some boilerplate config to it so we can start our server. This part of the config tells Vite where to look for our project’s JavaScript and how the development server should behave (pulling from the `src` folder with hot reload).
+1. **Open `vite.config.js` in your editor.** Since it's blank, we'll need to add some boilerplate config to it so we can start our server. This part of the config tells Vite where to look for our project's JavaScript and how the development server should behave (pulling from the `src` folder with hot reload).
```js
import { resolve } from 'path'
},
server: {
port: 8080
- },
- // Optional: Silence Sass deprecation warnings. See note below.
- css: {
- preprocessorOptions: {
- scss: {
- silenceDeprecations: [
- 'import',
- 'mixed-decls',
- 'color-functions',
- 'global-builtin',
- ],
- },
- },
- },
+ }
}
```
- **Note:** Sass deprecation warnings are shown when compiling source Sass files with the latest versions of Dart Sass. This does not prevent compilation or usage of Bootstrap. We’re [working on a long-term fix]([[config:repo]]/issues/40962), but in the meantime these deprecation notices can be ignored.
-
2. **Next we fill in `src/index.html`.** This is the HTML page Vite will load in the browser to utilize the bundled CSS and JS we'll add to it in later steps.
```html
</html>
```
- We’re including a little bit of Bootstrap styling here with the `div class="container"` and `<button>` so that we see when Bootstrap’s CSS is loaded by Vite.
+ We're including a little bit of Bootstrap styling here with the `div class="container"` and `<button>` so that we see when Bootstrap's CSS is loaded by Vite.
3. **Now we need an npm script to run Vite.** Open `package.json` and add the `start` script shown below (you should already have the test script). We'll use this script to start our local Vite dev server.
<img class="img-fluid" src="/docs/[[config:docs_version]]/assets/img/guides/vite-dev-server.png" alt="Vite dev server running" />
-In the next and final section to this guide, we’ll import all of Bootstrap’s CSS and JavaScript.
+In the next and final section to this guide, we'll import all of Bootstrap's CSS and JavaScript.
## Import Bootstrap
-1. **Import Bootstrap’s CSS.** Add the following to `src/scss/styles.scss` to import all of Bootstrap’s source Sass.
+1. **Import Bootstrap's CSS.** Add the following to `src/scss/styles.scss` to import all of Bootstrap's source Sass.
```scss
- // Import all of Bootstrap’s CSS
- @import "bootstrap/scss/bootstrap";
+ // Import all of Bootstrap's CSS
+ @use "bootstrap/scss/bootstrap";
```
*You can also import our stylesheets individually if you want. [Read our Sass import docs]([[docsref:/customize/sass#importing]]) for details.*
-2. **Next we load the CSS and import Bootstrap’s JavaScript.** Add the following to `src/js/main.js` to load the CSS and import all of Bootstrap’s JS. Floating UI will be imported automatically through Bootstrap.
+2. **Next we load the CSS and import Bootstrap's JavaScript.** Add the following to `src/js/main.js` to load the CSS and import all of Bootstrap's JS. Floating UI will be imported automatically through Bootstrap.
```js
// Import our custom CSS
import '../scss/styles.scss'
- // Import all of Bootstrap’s JS
+ // Import all of Bootstrap's JS
import * as bootstrap from 'bootstrap'
```
import { Tooltip, Toast, Popover } from 'bootstrap';
```
- *[Read our JavaScript docs]([[docsref:/getting-started/javascript/]]) for more information on how to use Bootstrap’s plugins.*
+ *[Read our JavaScript docs]([[docsref:/getting-started/javascript/]]) for more information on how to use Bootstrap's plugins.*
-3. **And you’re done! 🎉** With Bootstrap’s source Sass and JS fully loaded, your local development server should now look like this:
+3. **And you're done! 🎉** With Bootstrap's source Sass and JS fully loaded, your local development server should now look like this:
<img class="img-fluid" src="/docs/[[config:docs_version]]/assets/img/guides/vite-dev-server-bootstrap.png" alt="Vite dev server running with Bootstrap" />
- Now you can start adding any Bootstrap components you want to use. Be sure to [check out the complete Vite example project](https://github.com/twbs/examples/tree/main/vite) for how to include additional custom Sass and optimize your build by importing only the parts of Bootstrap’s CSS and JS that you need.
+ Now you can start adding any Bootstrap components you want to use. Be sure to [check out the complete Vite example project](https://github.com/twbs/examples/tree/main/vite) for how to include additional custom Sass and optimize your build by importing only the parts of Bootstrap's CSS and JS that you need.
<GuideFooter />
---
title: Bootstrap and Webpack
-description: The official guide for how to include and bundle Bootstrap’s CSS and JavaScript in your project using Webpack.
+description: The official guide for how to include and bundle Bootstrap's CSS and JavaScript in your project using Webpack.
toc: true
thumbnail: guides/bootstrap-webpack@2x.png
---
## Setup
-We’re building a Webpack project with Bootstrap from scratch, so there are some prerequisites and upfront steps before we can really get started. This guide requires you to have Node.js installed and some familiarity with the terminal.
+We're building a Webpack project with Bootstrap from scratch, so there are some prerequisites and upfront steps before we can really get started. This guide requires you to have Node.js installed and some familiarity with the terminal.
1. **Create a project folder and set up npm.** We'll create the `my-project` folder and initialize npm with the `-y` argument to avoid it asking us all the interactive questions.
npm i --save-dev webpack webpack-cli webpack-dev-server html-webpack-plugin
```
-3. **Install Bootstrap.** Now we can install Bootstrap. We'll also install Floating UI since our dropdowns, popovers, and tooltips depend on it for their positioning. If you don’t plan on using those components, you can omit Floating UI here.
+3. **Install Bootstrap.** Now we can install Bootstrap. We'll also install Floating UI since our dropdowns, popovers, and tooltips depend on it for their positioning. If you don't plan on using those components, you can omit Floating UI here.
```sh
npm i --save bootstrap @floating-ui/dom
```
-4. **Install additional dependencies.** In addition to Webpack and Bootstrap, we need a few more dependencies to properly import and bundle Bootstrap’s CSS and JS with Webpack. These include Sass, some loaders, and Autoprefixer.
+4. **Install additional dependencies.** In addition to Webpack and Bootstrap, we need a few more dependencies to properly import and bundle Bootstrap's CSS and JS with Webpack. These include Sass, some loaders, and Autoprefixer.
```sh
npm i --save-dev autoprefixer css-loader postcss-loader sass sass-loader style-loader
## Project structure
-We’ve already created the `my-project` folder and initialized npm. Now we'll also create our `src` and `dist` folders to round out the project structure. Run the following from `my-project`, or manually create the folder and file structure shown below.
+We've already created the `my-project` folder and initialized npm. Now we'll also create our `src` and `dist` folders to round out the project structure. Run the following from `my-project`, or manually create the folder and file structure shown below.
```sh
mkdir {src,src/js,src/scss}
touch src/index.html src/js/main.js src/scss/styles.scss webpack.config.js
```
-When you’re done, your complete project should look like this:
+When you're done, your complete project should look like this:
```text
my-project/
└── webpack.config.js
```
-At this point, everything is in the right place, but Webpack won’t work because we haven’t filled in our `webpack.config.js` yet.
+At this point, everything is in the right place, but Webpack won't work because we haven't filled in our `webpack.config.js` yet.
## Configure Webpack
With dependencies installed and our project folder ready for us to start coding, we can now configure Webpack and run our project locally.
-1. **Open `webpack.config.js` in your editor.** Since it’s blank, we'll need to add some boilerplate config to it so we can start our server. This part of the config tells Webpack where to look for our project’s JavaScript, where to output the compiled code to (`dist`), and how the development server should behave (pulling from the `dist` folder with hot reload).
+1. **Open `webpack.config.js` in your editor.** Since it's blank, we'll need to add some boilerplate config to it so we can start our server. This part of the config tells Webpack where to look for our project's JavaScript, where to output the compiled code to (`dist`), and how the development server should behave (pulling from the `dist` folder with hot reload).
```js
'use strict'
</html>
```
- We’re including a little bit of Bootstrap styling here with the `div class="container"` and `<button>` so that we see when Bootstrap’s CSS is loaded by Webpack.
+ We're including a little bit of Bootstrap styling here with the `div class="container"` and `<button>` so that we see when Bootstrap's CSS is loaded by Webpack.
3. **Now we need an npm script to run Webpack.** Open `package.json` and add the `start` script shown below (you should already have the test script). We'll use this script to start our local Webpack dev server. You can also add a `build` script shown below to build your project.
<img class="img-fluid" src="/docs/[[config:docs_version]]/assets/img/guides/webpack-dev-server.png" alt="Webpack dev server running"/>
-In the next and final section to this guide, we'll set up the Webpack loaders and import all of Bootstrap’s CSS and JavaScript.
+In the next and final section to this guide, we'll set up the Webpack loaders and import all of Bootstrap's CSS and JavaScript.
## Import Bootstrap
-Importing Bootstrap into Webpack requires the loaders we installed in the first section. We’ve installed them with npm, but now Webpack needs to be configured to use them.
+Importing Bootstrap into Webpack requires the loaders we installed in the first section. We've installed them with npm, but now Webpack needs to be configured to use them.
1. **Set up the loaders in `webpack.config.js`.** Your configuration file is now complete and should match the snippet below. The only new part here is the `module` section.
},
{
// Loads a SASS/SCSS file and compiles it to CSS
- loader: 'sass-loader',
- options: {
- sassOptions: {
- // Optional: Silence Sass deprecation warnings. See note below.
- silenceDeprecations: [
- 'mixed-decls',
- 'color-functions',
- 'global-builtin',
- 'import'
- ]
- }
- }
+ loader: 'sass-loader'
}
]
}
}
```
- Here’s a recap of why we need all these loaders. `style-loader` injects the CSS into a `<style>` element in the `<head>` of the HTML page, `css-loader` helps with using `@import` and `url()`, `postcss-loader` is required for Autoprefixer, and `sass-loader` allows us to use Sass.
-
- **Note:** Sass deprecation warnings are shown when compiling source Sass files with the latest versions of Dart Sass. This does not prevent compilation or usage of Bootstrap. We’re [working on a long-term fix]([[config:repo]]/issues/40962), but in the meantime these deprecation notices can be ignored.
+ Here's a recap of why we need all these loaders. `style-loader` injects the CSS into a `<style>` element in the `<head>` of the HTML page, `css-loader` helps with using `@import` and `url()`, `postcss-loader` is required for Autoprefixer, and `sass-loader` allows us to use Sass.
-2. **Now, let’s import Bootstrap’s CSS.** Add the following to `src/scss/styles.scss` to import all of Bootstrap’s source Sass.
+2. **Now, let's import Bootstrap's CSS.** Add the following to `src/scss/styles.scss` to import all of Bootstrap's source Sass.
```scss
- // Import all of Bootstrap’s CSS
- @import "bootstrap/scss/bootstrap";
+ // Import all of Bootstrap's CSS
+ @use "bootstrap/scss/bootstrap";
```
*You can also import our stylesheets individually if you want. [Read our Sass import docs]([[docsref:/customize/sass#importing]]) for details.*
-3. **Next we load the CSS and import Bootstrap’s JavaScript.** Add the following to `src/js/main.js` to load the CSS and import all of Bootstrap’s JS. Floating UI will be imported automatically through Bootstrap.
+3. **Next we load the CSS and import Bootstrap's JavaScript.** Add the following to `src/js/main.js` to load the CSS and import all of Bootstrap's JS. Floating UI will be imported automatically through Bootstrap.
```js
// Import our custom CSS
import '../scss/styles.scss'
- // Import all of Bootstrap’s JS
+ // Import all of Bootstrap's JS
import * as bootstrap from 'bootstrap'
```
import { Tooltip, Toast, Popover } from 'bootstrap'
```
- *[Read our JavaScript docs]([[docsref:/getting-started/javascript/]]) for more information on how to use Bootstrap’s plugins.*
+ *[Read our JavaScript docs]([[docsref:/getting-started/javascript/]]) for more information on how to use Bootstrap's plugins.*
-4. **And you’re done! 🎉** With Bootstrap’s source Sass and JS fully loaded, your local development server should now look like this:
+4. **And you're done! 🎉** With Bootstrap's source Sass and JS fully loaded, your local development server should now look like this:
<img class="img-fluid" src="/docs/[[config:docs_version]]/assets/img/guides/webpack-dev-server-bootstrap.png" alt="Webpack dev server running with Bootstrap"/>
- Now you can start adding any Bootstrap components you want to use. Be sure to [check out the complete Webpack example project](https://github.com/twbs/examples/tree/main/webpack) for how to include additional custom Sass and optimize your build by importing only the parts of Bootstrap’s CSS and JS that you need.
+ Now you can start adding any Bootstrap components you want to use. Be sure to [check out the complete Webpack example project](https://github.com/twbs/examples/tree/main/webpack) for how to include additional custom Sass and optimize your build by importing only the parts of Bootstrap's CSS and JS that you need.
## Production optimizations
### Extracting CSS
-The `style-loader` we configured above conveniently emits CSS into the bundle so that manually loading a CSS file in `dist/index.html` isn’t necessary. This approach may not work with a strict Content Security Policy, however, and it may become a bottleneck in your application due to the large bundle size.
+The `style-loader` we configured above conveniently emits CSS into the bundle so that manually loading a CSS file in `dist/index.html` isn't necessary. This approach may not work with a strict Content Security Policy, however, and it may become a bottleneck in your application due to the large bundle size.
To separate the CSS so that we can load it directly from `dist/index.html`, use the `mini-css-extract-loader` Webpack plugin.
### Extracting SVG files
-Bootstrap’s CSS includes multiple references to SVG files via inline `data:` URIs. If you define a Content Security Policy for your project that blocks `data:` URIs for images, then these SVG files will not load. You can get around this problem by extracting the inline SVG files using Webpack’s asset modules feature.
+Bootstrap's CSS includes multiple references to SVG files via inline `data:` URIs. If you define a Content Security Policy for your project that blocks `data:` URIs for images, then these SVG files will not load. You can get around this problem by extracting the inline SVG files using Webpack's asset modules feature.
Configure Webpack to extract inline SVG files like this:
use: [
```
-After running `npm run build` again, you’ll find the SVG files extracted into `dist/icons` and properly referenced from CSS.
+After running `npm run build` again, you'll find the SVG files extracted into `dist/icons` and properly referenced from CSS.
<GuideFooter />
margin-bottom: .25rem;
// stylelint-disable selector-max-type, selector-max-compound-selectors
- > p ~ ul {
+ > p ~ ul,
+ > p ~ p {
margin-top: -.5rem;
margin-bottom: 1rem;
}