Error: Angular encountered an undefined variable when attempting to import 'node_modules/bootstrap/scss/grid'

Having some trouble setting up Angular with SCSS and Bootstrap. When attempting to run ng serve, I encountered the following error:

./src/styles.scss - Error: Module build failed (from ./node_modules/sass-loader/dist/cjs.js):
SassError: Undefined variable.
    ╷
114 │       @each $key, $value in $gutters {
    │                             ^^^^^^^^
    ╵
  node_modules/bootstrap/scss/mixins/_grid.scss 114:29       @content
  node_modules/bootstrap/scss/mixins/_breakpoints.scss 68:5  media-breakpoint-up()
  node_modules/bootstrap/scss/mixins/_grid.scss 72:5         make-grid-columns()
  node_modules/bootstrap/scss/_grid.scss 32:3                @import
  src/scss/Custom.scss 11:9                                  @import
  src/styles.scss 2:9                                        root stylesheet

To replicate this issue, follow these steps:

  1. Create an Angular app using ng new <app name>
  2. npm i bootstrap
  3. Edit the angular.json file and add Bootstrap to the styles property:
    [...,"node_modules/bootstrap/scss/bootstrap.scss"]
  4. Add a src/scss/Custom.scss file and set it up as described at https://getbootstrap.com/docs/5.0/customize/sass/ (details below)
  5. Import
    @import "./scss/Custom.scss";
    into src/styles.scss.

Everything works fine when my src/scss/Custom.scss file is structured like this:

// 1. Include functions first (for color manipulation, SVGs, calc, etc)
@import "../../node_modules/bootstrap/scss/functions";

// // 2. Include any default variable overrides here

// // 3. Include remaining required Bootstrap stylesheets
@import "../../node_modules/bootstrap/scss/variables";
@import "../../node_modules/bootstrap/scss/mixins";

// // 4. Include optional Bootstrap components as needed
// @import "../../node_modules/bootstrap/scss/grid"; // Uncommenting this line triggers error

However, uncommenting the last line leads to the mentioned error.

Dependencies listed in Package.json include:

{
    ...
    "@angular/core": "^15.0.0",
    "bootstrap": "^5.2.3",

}

Answer №1

After encountering an issue, I managed to resolve it by incorporating bootstrap styles at the beginning of my styles.scss file:

@import 'node_modules/bootstrap/scss/bootstrap';
/* ...  */

Credit goes to @ColinM for the helpful tip.

Answer №2

I struggled to identify the issue at hand. I attempted to adhere to the instructions provided in the https://getbootstrap.com/docs/5.0/ documentation, but it appears that Angular compatibility may not be fully supported. Instead, opting for ng-bootstrap seems like a better solution.

Follow these steps in the CLI:

ng new my-ng-bootstrap-app # Create angular app. Select routing and SCSS preferences
ng add @ng-bootstrap/ng-bootstrap # Add bootstrap (refer to notes below on how bootstrap is integrated into the app)
ng generate component my-flex # Generate a new component to test bootstrap's flex styles
ng serve # Launch the Angular server

In later versions of Angular (specifically v14 onward), an error may arise. To resolve this, replace:

./src/styles.scss

// @import '~bootstrap/scss/bootstrap';
// Replace with
@import 'node_modules/bootstrap/scss/bootstrap';

With bootstrap configured, you can now visit https://getbootstrap.com/docs/5.0/layout/grid/ and copy the code snippets.

src/app/my-flex/my-flex.component.html

  <!-- Insert example code from https://getbootstrap.com/docs/5.0/layout/grid/#example here -->
<div class="container">
  <div class="row">
    <div class="col">
      Column
    </div>
    <div class="col">
      Column
    </div>
    <div class="col">
      Column
    </div>
  </div>
</div>

src/app/app.component.html

<!-- Erase previous code -->
<app-my-flex></app-my-flex>

Once completed, everything should function correctly. Verify in your browser’s developer tools that the DOM elements exhibit the applied bootstrap styles.

(FYI)

ng add @ng-bootstrap/ng-bootstrap
inserts the following into:

src/styles.scss

/* Importing Bootstrap SCSS file. */
@import '~bootstrap/scss/bootstrap';

src/app/app.module.ts

import { NgbModule } from '@ng-bootstrap/ng-bootstrap';
...
@NgModule({
  ...
  imports: [
    ...
    NgbModule
  ],
  bootstrap: [AppComponent]
})

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best way to position images and URLs in div elements using CSS?

Currently, I am on a journey to become a Front End Developer through Udacity. As part of my learning process, I have a project that needs to be submitted. Unfortunately, I encountered some issues that I couldn't resolve on my own. Specifically, I&apos ...

Enhance your React project by incorporating Material-UI card media elements with the ability to add

I am trying to figure out how to create an opaque colored overlay on top of an image using Material-UI. While it is possible with plain HTML, CSS, and JavaScript, I am facing some challenges with Material-UI. <Card> <CardMedia> <im ...

Tips on making a dropdown select menu expand in a downward direction

I'm currently using a select element to choose options from a dropdown list, but because of the large number of items, the list displays in an upward direction instead of downwards. I am working with Bootstrap. I have reviewed other code samples with ...

Navigating to the most recent item within ng-repeat (AngularJS or JavaScript)

I am working on a feature where posts are displayed using ng-repeat in a div, and users can enter new posts in an input box. The posts are sorted so that the latest one appears at the bottom. After adding a new post, I want to automatically scroll down t ...

Disable Button's Shadow when it is in an active state (clicked)

Check out the DEMO to see the button animation CSS in action. The CSS code for the button animations is as follows: .btnliner { /* CSS properties */ } /* More CSS properties */ .btnliner:hover { /* Hover effects */ } Here is the corresponding J ...

What happens if I define both the left and right properties for spans?

This is a class for span with the CSS properties position:absolute; top:0px; left:0px; right:0px; .textStyle1 { font-size:24px; font-family:'Arial'; position:absolute; top:0px; left:0px; right:0px; } #div1 { position:absolute; lef ...

What is the proper way to tap into the features provided by DefinitelyTyped in typescript?

While working on my Angular2 app that deals with money amounts, I decided to use dinero.js to handle money values. However, I am encountering difficulties in accessing certain features in Typescript. Following the instructions, I installed the DefinitelyT ...

Calculating the length of time based on a specific date in Angular 2+: A step-by-step

API: { data:{ "duration": 12, "startDate": "27-01-2020 16:09" } } I am currently working with Angular 2+. In the API response, the duration is set to 12 (in months) and the start date is provided as well... Task at hand: My goal is to ...

Div that scrolls independently without affecting the position of other elements

Visit this link for reference <div class="col-10 content" id="content"> <ul> <li>News</li> <li>News</li> <li>News</li> <li>News</li> < ...

Exploring Angular 8 Route Paths

Working on an Angular 8 project, I encountered an issue with my code: src/app/helpers/auth.guard.ts import { AuthenticationService } from '@app/services'; The AuthenticationService ts file is located at: src/app/services/authentication.servic ...

Encountered a Webpack issue when trying to load the primeng.min

I recently initiated a fresh project using yo aspnetcore-spa. My goal is to integrate the PrimeNG component library. Upon installing font-awesome and primeng: npm install font-awesome primeng --save I included CSS in the webpack vendor list: vendor: [ ...

Proper Placement of Required Field Validator Messages

I am having an issue with the validation text appearing behind my textbox instead of below it. Despite setting the display to block in the CSS class for the assignment type and start date, the validation text is not displaying properly. I have tried variou ...

Is there a way to alter a class using ng-class only when the form is deemed valid?

I am trying to implement a feature where an input field shows as required, but once the user enters text, it indicates that the input is valid by changing the border color from red to green. I am facing an issue with this line of code always returning fal ...

What are the steps for manually integrating Bootstrap into an Angular project?

I'm currently working on an Angular 5 project within a private domain where I am unable to utilize the npm-install command. As a result, I have manually added Bootstrap's CSS and JS files to my project. I am now unsure how to properly link these ...

Setting a default value in a select tag with ngModel in Angular 6

I am currently working with Angular 6 and have the following code snippet: <select class="form-control form-control-sm" #ig="ngModel" [(ngModel)]="assetFormDetails.ig"> <option value="" >S ...

Animate images using mouse vertical movement

Creating websites is just a hobby for me, not something professional. Recently, I came across a beautifully designed website that had some unique features I hadn't seen before, like placing three images in one div (which I researched and learned how t ...

The inline feature of the Bootstrap input group addon is not being utilized

Check out my code snippet here: http://www.bootply.com/iR1SvOyEGH <form> <div class="form-group"> <label for="inputEmail">Company Name</label> <input type="text" class="form-control"> <span class="input-gro ...

Calculate the quantity of rows within a specific div container

Looking at the image provided, there are multiple divs inside a main div. I am trying to figure out how many rows the main div contains. For instance, in this particular scenario, there are 5 rows. It is important to mention that the inner div is floated ...

Display the datepicker beneath the input field

I successfully integrated the datepicker, but I prefer for the calendar to display below the date input field rather than above it. HTML5 <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv=" ...

I'm having trouble viewing the unique Google Map design on my application

I have recently customized Google maps following the guidelines in this documentation: https://developers.google.com/maps/documentation/javascript/styling For styling, I utilized the Cloud tool and opted for the available template instead of using JSON st ...