Transitioning to utilizing Bootstrap through npm

I am currently working on a project that utilizes Bootstrap. However, the Bootstrap files are being loaded into a subdirectory. I would like to transition to using npm for easier maintenance and updates.

Within our existing style folder, we have subfolders for bootstrap and source, along with an index.js file:

var ms = require('ms');
var join = require('path').join;
var less = require('transform')('less');
var express = require('express');
var app = express();

.............*other code*

app.get('/bootstrap.css', less(join(__dirname, 'source',
                                    process.env.COMPANY_ID + '-root.less')));
app.get('/print.css', less(join(__dirname, 'source',
                                process.env.COMPANY_ID + '-print.less')));

module.exports = app;

The source folder contains several .less files, including a company-root.less which imports various other less files:

@import "./bootstrap/bootstrap.less";    
@import "./variables/all.less";
@import "./variables/company.less";   
@import "./customcss.less";    
@import "./subnav.less";    
@import "./nav-list.less";

I have added Bootstrap to package.json and ran npm install, which places Bootstrap in the node_modules folder. However, I am experiencing issues getting the project to run with these new files. I tried adding

var $ = require('jQuery');
global.jQuery = $;
require('bootstrap');

to index.js. This resulted in an error:

TypeError: Cannot set property 'emulateTransitionEnd' of undefined
    at C:\Users\Dee\Documents\GitHub\MAPS\node_modules\bootstrap\js\transition.js:36:29

I also attempted changing the first line of company-root.js to:

@import (npm) "bootstrap";

OR

@import "../../node_modules/bootstrap/less/bootstrap.less";

Both of these changes led to no formatting being applied. Despite researching online for three hours, I am unable to make any progress. Can someone please offer assistance?

Answer №1

To incorporate Bootstrap into your project, consider using a tool like bootstrap-sass-loader or bootstrap-webpack.

While these solutions work for most scenarios, they may not be compatible with all webpack setups.

global.jQuery = global.$ = require('jquery');
require('bootstrap');

Answer №2

Encountering the same error, I found a solution while working on a small demo that required bootstrap in the HTML. I attempted to load bootstrap in the .js file using require(bootstrap), resulting in the issue.

To resolve it, I eliminated the require(bootstrap) from the .js file and simply added bootstrap to the <head> section of my HTML like this:

<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css">
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"></script>

The content of my .js file now includes:

var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
global.jQuery = require('jquery');

After running the project with these adjustments, the error disappeared, and everything functioned smoothly. Sharing this solution here in case it may help others facing the same error message.

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

Risk Score Generating Form

As I work on a comprehensive form, there are 5 sections for users to mark if they qualify (using checkmarks). The form consists of approximately 50 questions in total. Each section carries different weights/points (e.g., Section 1 is worth 1 point each, ...

Tips for creating multiple popups using a single line of JavaScript code

I am new to JavaScript and I am attempting to create a popup. However, I am facing an issue in opening two divs with a single line of JavaScript code. Only one div opens while the other remains closed despite trying various solutions found on this website. ...

Discover the process for breaking down JSON data into separate stages using the Express API

My API architecture is causing a problem as I try to insert it into an array called items[]. https://i.stack.imgur.com/qe802.png The setup involves a simple API built on express + mongodb. The challenge lies in figuring out how to store data from the pos ...

What is the method to identify the test case name as passed or failed in Puppeteer?

I am currently working on integrating the puppeteer-jest test framework with TestRail using TestRail API. To accomplish this task, I need to identify which tests have failed and which tests have passed. Despite searching through the official GitHub Reposi ...

Delete the query parameter from the router

In my Vue project, I am using the replace method to add a query to the current URL. If the value is not present, I'm assigning undefined. this.$router.replace({ name: "admin-frs", query: { limit: this.pageSize, page: this.current ...

Combining data from an element in a list with a state object in Redux

While experimenting with redux and reactjs, I came across an interesting scenario: I have a collection of items const items = [ {id: 0, name: 'one'}, {id: 1, name: 'two'}, {id: 2, name: 'three'} ] const itemsWithTheFl ...

Looking for assistance with the installation of Bitcoin Core?

Currently, I am encountering this error message: npm WARN enoent ENOENT: no such file or directory, open 'C:\Users\eze samuel\package.json' npm WARN eze samuel No description npm WARN eze samuel No repository field. npm WARN eze s ...

What is the best approach to handle Flow types for component props and getDerivedStateFromProps when the props are not the same

Having a Component with its props, an additional prop is added for getDerivedStateFromProps. The issue arises when setting the props with the additional one, throwing an error that the prop is not being used. Conversely, setting it without the extra prop c ...

What is the best way to duplicate an entire webpage with all its content intact?

Is it possible to copy an entire page including images and CSS using Selenium? Traditional methods like ctrl + a or dragging the mouse over the page do not seem to work. How can this be achieved with Selenium without requiring an element to interact with? ...

I encountered an error message while running the Angular JS code that I had written, and I am unable to find a solution to resolve it

Every time I attempt to run ng serve, the following error message pops up: "The serve command requires to be run in an Angular project, but a project definition could not be found." I've already experimented with various commands like: npm cache clean ...

Struggling to align elements in React/JS/M?

My challenge is aligning the elements in the middle of my page. I aim to have 3 elements per row, with n rows depending on the number of "VideoBox" components. It's crucial that these elements fit within the lettering of the P R O F E S S I O N A L ...

Tips for utilizing AJAX to send data from a specific row

<table> <tr data-id="1"> <input type="text" name="name_1" value="abc"> <input type="text" name="value_1" value="1"> <a href="load_edit_row(this)">Edit</a> </tr> <tr data-id="2"> <input t ...

What is the correct way to implement a text field using jQuery?

Having trouble getting a text field to show up on the stage of the pong game with the code below. Looking for some assistance with this, as it's my first attempt at using jquery and javascript. Thanks in advance for your help! <!doctype html& ...

Implementing dynamic data filtering in Vue.js is a valuable feature that

I'm working on a Vuejs project where I need to filter out only the questions with reviewed:true and get the count of them. However, I'm encountering an error while using the code below: TypeError: question.reviewed.includes is not a function. Can ...

Undefined Javascript variables are commonly found within loops

My code includes a loop that fetches an array of users and attempts to retrieve the information of each user. However, I've encountered an issue where if there are multiple users, it ends up returning the same information (specifically, the details of ...

ASP.Net - Unexpected JSON Format Error

As I work on my ASP.Net web application, I am encountering an issue with binding data from a database to a Google Combo chart via a Web Service class. While I can successfully bind the data to a grid view, attempting to bind it to the chart results in the ...

Prevent Purchase Button & Implement Modal on Store Page if Minimum Requirement is not Achieved

On my woocommerce shop page, I am facing an issue where all items are added to the mini-cart without meeting the minimum order requirement. This results in users being able to proceed to checkout without adding enough items to meet the minimum order amount ...

What is the reason behind "npm update" only updating the module(s) and not its SemVer in package.json?

My "package.json" file shows an old version of the express module - "4.10.0" https://i.stack.imgur.com/xyEDz.png The command "npm outdated" suggests updating to version "4.17.1" https://i.stack.imgur.com/rmqWJ.png Even after running "npm update" to ver ...

What causes the error when I use "use client" at the top of a component in Next.js?

Whenever I include "use client" at the top of my component, I encounter the following error: event - compiled client and server successfully in 2.5s (265 modules) wait - compiling... event - compiled client and server successfully in 932 ms (265 modules) ...

Use jquery ajax to upload an image with a reusable input field

UPDATE: Progress has been made in solving this issue. Please refer to Jquery form no submission to IE7 and IE8. The main task remaining is sorting out the compatibility with IE7 and IE8. I have been utilizing THIS plugin to upload files as email attachmen ...