Bootstrap and npm
The official guide for how to build a starter project with Bootstrap’s CSS and JavaScript in your project using just npm.
Want to skip to the end? Download the source code and working demo for this guide from the twbs/examples repository. You can also open the example in StackBlitz.
What is npm?
npm 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.
-
Create a project folder and set up npm. We’ll create the
my-projectfolder and initialize npm with the-yargument to avoid it asking us all the interactive questions.mkdir my-project && cd my-project npm init -y -
Install Bootstrap. Now we can install Bootstrap. We’ll also install Floating UI and Vanilla Calendar Pro since some of our components depend on them. If you don’t plan on using menus, popovers, or tooltips, you can omit Floating UI. If you don’t plan on using the datepicker, you can omit Vanilla Calendar Pro.
npm i --save bootstrap @floating-ui/dom vanilla-calendar-pro -
Install additional dependencies. In addition to Bootstrap, we need a few more dependencies to compile and post-process our CSS. Sass compiles our source
.scssfiles into CSS, and Autoprefixer and PostCSS handle browser compatibility. We install thepostcss-clipackage so we can run PostCSS from the command line.npm i --save-dev autoprefixer postcss postcss-cli sass -
Install local development tools. Lastly, to make life a little easier, we recommend installing some dev tools to make running local servers with hot refresh (where changes you make are reflected in the browser automatically) and add some code quality checks.
We use
nodemonto watch files for changes,npm-run-allto run multiple npm scripts at a time,sirv-clito run a local server, andstylelintto format CSS like Bootstrap does for consistency.npm i --save-dev nodemon npm-run-all sirv-cli stylelint stylelint-config-twbs-bootstrap
Now that we have all the necessary dependencies installed, we can get to work creating the project files and importing Bootstrap.
Project structure
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.
mkdir {css,scss}
touch index.html scss/styles.scss postcss.config.js .stylelintrc.jsonWhen you’re done, your project should look like this:
my-project/
├── css/
├── scss/
│ └── styles.scss
├── .stylelintrc.json
├── index.html
├── package-lock.json
├── package.json
└── postcss.config.jsAt 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.
-
Open
postcss.config.jsin your editor. We need to configure Autoprefixer to run after Sass compilation for browser compatibility.const autoprefixer = require('autoprefixer') module.exports = { plugins: [ autoprefixer ] } -
Fill in the
index.htmlfile. 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.<!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 type="module" 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 for more information on how to use Bootstrap’s plugins.
-
Add npm scripts to
package.json. Openpackage.jsonand add the following scripts. We usesassto compile our Sass (with--load-path=node_modulesso it can resolve Bootstrap),postcss-clito apply Autoprefixer,nodemonto watch for changes,npm-run-allto run scripts in sequence or in parallel,sirv-clito serve our project, andstylelintto lint our Sass.{ // ... "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\"" }, // ... } -
Configure
.stylelintrc.json. We’re usingstylelint-config-twbs-bootstrapto keep our Sass linting consistent with Bootstrap’s own code style.{ "extends": "stylelint-config-twbs-bootstrap" } -
And finally, we can start the npm scripts. From the
my-projectfolder in your terminal, run that newly added npm script:npm start
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.
-
Import Bootstrap’s CSS. Add the following to
scss/styles.scssto import all of Bootstrap’s source Sass. We use the@userule, which is the modern Sass module system.// 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 for details.
-
Optionally, customize Bootstrap’s Sass tokens. Since Bootstrap uses Sass modules (
@use/@forward), you can override default values using thewith ()syntax. You only need to include the keys you want to change — Bootstrap merges your overrides with its defaults.@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 for the full list of available token maps and options.
-
And you’re done! 🎉 With Bootstrap’s source Sass fully loaded, your local development server should now look like this:
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 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.
See something wrong or out of date here? Please open an issue on GitHub. Need help troubleshooting? Search or start a discussion on GitHub.