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.

  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.

    Shell
    mkdir my-project && cd my-project
    npm init -y
  2. 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.

    Shell
    npm i --save modus-bootstrap @floating-ui/dom vanilla-calendar-pro
  3. 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 .scss files into CSS. PostCSS then adds the bs- prefix to CSS custom properties and Autoprefixer handles browser compatibility. We install the postcss-cli package so we can run PostCSS from the command line.

    Shell
    npm i --save-dev autoprefixer postcss postcss-cli postcss-prefix-custom-properties sass

    The postcss-prefix-custom-properties plugin 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.

  4. 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 nodemon to watch files for changes, npm-run-all to run multiple npm scripts at a time, sirv-cli to run a local server, and stylelint to format CSS like Modus Bootstrap does for consistency.

    Shell
    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.

Shell
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:

TEXT
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.

  1. Open postcss.config.js in your editor. After Sass compiles, PostCSS must prefix CSS custom properties with bs- and then run Autoprefixer. Match the options Modus Bootstrap uses in its own build so JavaScript plugins keep working.

    JavaScript
    const autoprefixer = require('autoprefixer')
    const postcssPrefixCustomProperties = require('postcss-prefix-custom-properties')
    
    module.exports = {
      plugins: [
        postcssPrefixCustomProperties({
          prefix: 'bs-',
          ignore: [/^--bs-/, /^--bd-/]
        }),
        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">
        <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.

  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 Modus Bootstrap), postcss-cli to prefix CSS variables and 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 stylelint.config.mjs. We’re using stylelint-config-twbs-bootstrap to keep our Sass linting consistent with Modus Bootstrap’s own code style.

    JavaScript
    /** @type {import('stylelint').Config} */
    export default {
      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:

    Shell
    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.

  1. Import Modus Bootstrap’s CSS. Add the following to scss/styles.scss to import all of Modus Bootstrap’s source Sass. We use the @use rule, which is the modern Sass module system.

    SCSS
    // 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.

  2. Optionally, customize Modus Bootstrap’s Sass tokens. Since Modus 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 — Modus 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 for the full list of available token maps and options.

  3. 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.