CoreUI & npm
The official guide for how to build a starter project with CoreUI’s CSS and JavaScript using just npm — no bundler required.
What is npm?
npm is the default package manager for Node.js. In this guide, we use npm to install CoreUI and its dependencies, then compile Sass to CSS using command-line tools — no bundler required. If you’d rather have a bundler manage the assets, see our Vite, Webpack or Parcel guides.
Setup
We’re building a npm project with CoreUI 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.
-
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 CoreUI. Now we can install CoreUI. 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.
npm i --save @coreui/coreui @floating-ui/domFor PRO users
npm i --save @coreui/coreui-pro @floating-ui/dom -
Install additional dependencies. In addition to CoreUI, 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 some dev tools for running a local server with automatic rebuilds. We use
nodemonto watch files for changes,npm-run-allto run multiple npm scripts at a time,sirv-clito run a local server, andstylelintto keep your Sass consistent with the style CoreUI is written in.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 CoreUI.
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.mjsWhen you’re done, your project should look like this:
my-project/
├── css/
├── scss/
│ └── styles.scss
├── index.html
├── package-lock.json
├── package.json
├── postcss.config.js
└── stylelint.config.mjsAt 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 ] } -
Tell Autoprefixer which browsers to support. Without this, Autoprefixer falls back to its own defaults, which track newer browsers than the ones CoreUI supports — and quietly skips prefixes the floor still needs, such as
-webkit-backdrop-filterfor Safari below 18. Add abrowserslistentry topackage.jsonmirroring our floors:{ // ... "browserslist": [ "Chrome >= 123", "Edge >= 123", "Firefox >= 130", "Safari >= 17.5", "iOS >= 17.5" ], // ... } -
Fill in the
index.htmlfile. We need an HTML page to render our project. This page links to the compiled CSS file and includes CoreUI’s pre-built JavaScript bundle. Since this setup doesn’t use a JavaScript bundler, we load the bundle — which already contains Floating UI — directly via a<script>tag.<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>CoreUI w/ npm</title> <link rel="stylesheet" href="css/styles.css"> </head> <body> <div class="container py-5 px-4 mx-auto"> <h1>Hello, CoreUI and npm!</h1> <button class="btn-solid theme-primary">Primary button</button> </div> <script type="module" src="node_modules/@coreui/coreui/dist/js/coreui.bundle.min.js"></script> </body> </html>We’re including a little bit of CoreUI styling here with the
div class="container"and<button>so that we see when CoreUI’s CSS is loaded by our npm scripts.Read our JavaScript docs for more information on how to use CoreUI’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 CoreUI),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
stylelint.config.mjs.stylelint-config-twbs-bootstrapis the shared config CoreUI’s own stylesheets are linted with, so your Sass follows the same rules as the source you are extending./** @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 CoreUI’s CSS.
Import CoreUI
Importing CoreUI into our project requires adding a Sass import to our scss/styles.scss file.
-
Import CoreUI’s CSS. Add the following to
scss/styles.scssto import all of CoreUI’s source Sass. We use the@userule, which is the modern Sass module system.// Use all of CoreUI's CSS @use "@coreui/coreui/scss/coreui";PRO users import the PRO package instead. Import one of the two — importing both emits the whole stylesheet twice.
@use "@coreui/coreui-pro/scss/coreui";You can also import our stylesheets individually if you want. Read our Sass import docs for details.
-
Optionally, customize CoreUI’s Sass tokens. Since CoreUI uses Sass modules, you can override default values using the
with ()syntax. You only need to include the keys you want to change — CoreUI merges your overrides with its defaults.@use "@coreui/coreui/scss/coreui" with ( $root-tokens: ( --cui-radius-5: .25rem, --cui-spacer: 1.5rem, ), $alert-tokens: ( --cui-alert-padding-x: 2rem, --cui-alert-border-radius: 1rem, ) );Read Sass for how overrides merge with defaults, and Architecture for all the levels an override can live at.
-
And you’re done! 🎉 With CoreUI’s source Sass fully loaded and compiled at
http://localhost:8080, you can start adding any CoreUI components you want to use.