Modus Bootstrap and npm
The official guide for how to build a starter project with Modus Bootstrap’s CSS and Bootstrap’s JavaScript in your project using just npm.
Using an AI coding agent? Point it at our bootstrap-npm skill to automate this setup. See AI for all available skills.
What is npm?
npm is the default package manager for Node.js. In this guide, we use npm to install Modus Bootstrap and its dependencies, then compile Sass to CSS using command-line tools—no bundler required.
Setup
We’re building a npm project with Modus 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 Modus Bootstrap. Now we can install Modus 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 modus-bootstrap @floating-ui/dom vanilla-calendar-pro -
Install additional dependencies. In addition to Modus Bootstrap, we need a few more dependencies to compile and post-process our CSS. Sass compiles our source
.scssfiles into CSS. PostCSS then adds thebs-prefix to CSS custom properties and Autoprefixer handles browser compatibility. We install thepostcss-clipackage so we can run PostCSS from the command line.npm i --save-dev autoprefixer postcss postcss-cli postcss-prefix-custom-properties sassThe
postcss-prefix-custom-propertiesplugin is required when you compile Modus Bootstrap’s Sass yourself. Our JavaScript plugins read prefixed names such as--bs-range-fill. Without this plugin, those names stay unprefixed in your CSS and the plugins stop matching them. See CSS variables for details. -
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 Modus 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 Modus 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 stylelint.config.mjs
When you’re done, your project should look like this:
my-project/
├── css/
├── scss/
│ └── styles.scss
├── stylelint.config.mjs
├── index.html
├── package-lock.json
├── package.json
└── postcss.config.js
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.
-
Open
postcss.config.jsin your editor. After Sass compiles, PostCSS must prefix CSS custom properties withbs-and then run Autoprefixer. Match the options Modus Bootstrap uses in its own build so JavaScript plugins keep working.const autoprefixer = require('autoprefixer') const postcssPrefixCustomProperties = require('postcss-prefix-custom-properties') module.exports = { plugins: [ postcssPrefixCustomProperties({ prefix: 'bs-', ignore: [/^--bs-/, /^--bd-/] }), 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"> <title>Modus Bootstrap w/ npm</title> <link rel="stylesheet" href="css/styles.css"> </head> <body> <div class="container py-4 px-3 mx-auto"> <h1>Hello, Modus Bootstrap and npm!</h1> <button class="btn-solid theme-primary">Primary button</button> </div> <script type="module" src="node_modules/modus-bootstrap/dist/js/bootstrap.bundle.min.js"></script> </body> </html>We’re including a little bit of Modus Bootstrap styling here with the
div class="container"and<button>so that we see when Modus 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 Modus Bootstrap),postcss-clito prefix CSS variables and 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
stylelint.config.mjs. We’re usingstylelint-config-twbs-bootstrapto keep our Sass linting consistent with Modus Bootstrap’s own code style./** @type {import('stylelint').Config} */ export default { 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 Modus Bootstrap’s CSS.
Import Modus Bootstrap
Importing Modus Bootstrap into our project requires adding a Sass import to our scss/styles.scss file.
-
Import Modus Bootstrap’s CSS. Add the following to
scss/styles.scssto import all of Modus Bootstrap’s source Sass. We use the@userule, which is the modern Sass module system.// Import all of Modus 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 Modus Bootstrap’s Sass tokens. Since Modus 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 — Modus 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 Modus Bootstrap’s source Sass fully loaded, your local development server should now look like this:
Now you can start adding any Modus 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 Modus 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.