What steps are needed to successfully integrate bootstrap.js, jquery, and popper.js into a project by using NPM to add Bootstrap?

I've successfully set up a project and incorporated Bootstrap via npm. However, I'm facing challenges in loading the necessary javascript files for Bootstrap components. It's important to note that I am utilizing a Vagrant Box (virtual machine) to host the project. Additionally, I have browser-sync plugin configured which initiates a mini-server referencing the one from the VM. Hopefully, my explanation is clear!

Index file (located in the public folder)

<div class="carousel-inner" data-slide-to="2">
      <div class="carousel-item active">
        <img class="d-block w-100 " src="images/logo.jpeg" alt="First slide">
      </div>
      <div class="carousel-item">
        <img class="d-block w-100" src="images/01.jpeg" alt="Second slide">
      </div>
      <div class="carousel-item">
        <img class="d-block w-100" src="images/02.jpeg" alt="Third slide">
      </div>
    </div>
  </div>
  
  <script src="./node_modules/jquery/dist/jquery.js"></script>
  <script src="./node_modules/popper.js/dist/umd/popper.js"></script>
  <script src="./node_modules/bootstrap/dist/js/bootstrap.js"></script>
</body>
</html>

Package.json file

{
  "name": "bstrap",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "build-task:scss-compile": "node-sass-chokidar --source-map true scss -o public/css",
    "build-task:autoprefixer": "postcss public/css/*.css --use autoprefixer postcss-nested -d public/css",
    "sass:build": "npm-run-all -p build-task:*",
    "sass:watch": "chokidar \"scss/**/*.scss\" -c \"npm run sass:build\"",
    "sass:browsersync": "browser-sync start --proxy \"strap.test\" --files \"public\"",
    "dev": "npm-run-all -p sass:*"
  }

Console errors

https://i.sstatic.net/TiN4c.png

Answer №1

Typically, when working with NPM and Bootstrap, I avoid deploying the node_modules folder to the server in production or staging environments. Instead, for local projects, I prefer to import these node modules into my main JavaScript source file and compile them to a distribution folder.

If you are directly referencing the node_modules directory as you are doing currently, you will need to upload the entire node_modules folder to your production or staging environments, which may not be the most efficient approach.

The way I handle importing jQuery, Bootstrap, and Popper into my src/js/theme.js file, which is then compiled to dist/js/theme.js, is as follows...

// Load jQuery
import jQuery from 'jquery';
window.jQuery = window.$ = jQuery;

// Include Bootstrap and Popper
require('bootstrap');
require('popper.js');

// Add all your custom JavaScript here...

Finally, you can simply include the compiled theme.js file in your project like this...

<script src="/dist/js/theme.js"></script>

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

How to embed HTML code within PHP using a conditional variable

I've encountered an issue with a PHP code that sends emails containing HTML templates. The HTML template includes PHP variables such as Name and Email. <strong>Name: {name} </strong> As the email template is lengthy, I have included ...

How to disable the Rubber/Elastic scrolling feature on your iPad?

On my webpage, there is a combination of vertical and horizontal scrolling. Everything works fine when the user swipes left or right exactly, but issues arise when the swipe is not precise along a straight line (diagonally). In this case, both vertical and ...

Steps for updating a JSON entry in a Node.js environment

What is the best way to update a JSON value using node.js? I have come across a few examples online, but my situation is a bit more complex. I am able to access the value that needs to be modified var contents = fs.readFileSync("./../../skill.json"); var ...

CSS for mobile devices not loading until the device is rotated

My website, redkrypt.com, is functioning perfectly on my laptop. However, I am facing an issue where the css file will only load on my iPhone 4S after I rotate it once. Once I do this, the website works both vertically and horizontally. I have tried variou ...

Fetching Information from Firebase and Populating Dropdown Menus on a Website

Displayed below is a code snippet from a .JSON file stored in a Firebase database. "questionnaires" : { "ORANGE" : { "location" : "ny", "major" : { "engineering" : { "ECE" : { "3rd sem" : { "2018 ...

Is it better to have JavaScript and HTML in separate files or combined into a single HTML file?

Do you notice any difference when coding in both HTML and JavaScript within a single .html file compared to separating HTML code in a .html file and JavaScript code in a .js file? Does the functionality remain the same in both scenarios? For example, in t ...

Transform the structure of an object using Ramda from its original form

Is it possible to transform an object by modifying and filtering it to create a different shape that is easier to work with after making an API request? I've been struggling to find elegant solutions that don't involve using path and prop for eve ...

The expected functionality of setTimeout is not being achieved

I have implemented an ajax scroll down feature in my application to load user posts. However, I am facing an issue where the for loop iteration is causing excessive loading time, leading to browser freezing until the results are displayed. To address this ...

The error "Call to a member function exports() on array" occurs when attempting to use the

As I attempt to send an array of values to the jobExport() collection, I encounter an error stating Call to a member function jobsExport() on array. I comprehend the necessity for the collection to be populated with modal collection value. However, my goal ...

Populating a Listview in jqueryMobile with dynamic elements

I am struggling with my listview. Whenever I try to add or remove items from the list, the jquery mobile styling does not get applied to the new content that is added. <ul data-role="listview" id="contributionList"> <li id="l1"><a>5. ...

When a text is wrapped by an HTML div using absolute positioning, is there a way to prevent it from wrapping without relying on white space

I have a group of div elements positioned absolutely on the screen. If any div contains content that is too big for the screen, the browser wraps it into multiple lines to fit. However, I do not want this behavior. Instead, I want the overflowing content ...

Executing asynchronous Django AJAX calls on a single URL

I'm encountering an issue with submitting forms in Django. I have two forms, each needing to insert a specific number into the database. Form1: should insert value 1 Form2: should insert value 2 Both forms are located at the URL /kategorije/. When ...

The fancybox scroll feature is visible when using a browser, but unfortunately, it does not appear on the

I noticed that the scrollbar in my fancybox is not showing up on iPad, but it does display when using Google Chrome on Desktop. You can find the page here. The issue seems to be with the vertical scroll bar not appearing on iPad. Has anyone encountered th ...

Customizing Angular 2's Webpack environment setup dynamically

Currently, I have set up Webpack to compile my Angular project. Within my project, there is an important file called env.js: module.exports.ENV = { API_URL: "http://localhost:5001/", ... }; In the webpack.common.js file, I am referencing this file l ...

What is the most effective way to extract the output from a JSONP request and utilize it beyond the

I have a function that is working well. I had to use JSONP to handle cross-domain issues, and I also wrote an HTTP module to change the content-type. However, I did not include a callback name in the URL. function AddSecurityCode(securityCode, token) { va ...

Exploring JSON array handling with jquery

Here is the JSON data I am working with: { "category": { "category_identification": "1", "category_name": "C1", "image_one": "1.PNG", "image_two": "1_.PNG", "logo": "LOGO_1.PNG", "category_description": "DESCRIPTION" }, "responseCo ...

I need help getting rid of the unwanted text and objects that are appearing on my website

I recently designed a basic website and everything was running smoothly until I decided to insert a new ul division in the HTML page as a spacer between two elements: <div> <ul> <li><a></a></li> </u ...

Guide on extracting JSON data from specific nodes using d3.js

Just starting out with d3.js and following the example of a simple molecule created by d3 at http://bl.ocks.org/mbostock/3037015. I have a couple questions: 1. How can I select multiple nodes within the molecule structure? 2. Once selected, how do I ext ...

CSS: Turn off the custom styling for radio buttons

My dilemma lies in the custom radio button I've created using CSS. The issue is that while I can select the radio button, I cannot deselect it. Here is a snippet of the CSS code I used to create the custom radio button: input[type="radio"]:checked:be ...

The JSON response is not being returned by the static React App hosted on Microsoft

Seeking assistance from anyone who may have faced and resolved a similar issue. Our React application is deployed on Azure Static Web App and operates smoothly, but we are stuck on configuring it to return JSON instead of HTML in its responses. Within our ...