Issue with compiling Bootstrap 4 TypeScript due to missing module 'popper.js'

While setting up my TypeScript project in Visual Studio Code, I encountered an issue trying to incorporate bootstrap 4 along with Popper, jQuery, and Knockout.

I made sure to install the type definitions for knockout, jquery, and bootstrap using these commands:

npm install -–save @types/knockout
npm install -–save @types/jquery
npm install --save @types/bootstrap

Additionally, I referenced the required JS files in a require.js configuration:

declare var require: any;
require.config({
    paths: {
        "knockout": "externals/knockout-3.5.0",
        "jquery": "externals/jquery-3.3.1.min",
        "popper.js": "https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js",
        "bootstrap": "https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"
    }
})

My tsconfig.json looks like this:

{
    "compilerOptions": {
        "baseUrl": ".",
        "paths": { 
            "*": ["types/*"] 
        },
        "outDir": "./built/",
        "sourceMap": true,
        "noImplicitAny": true,
        "module": "amd",
        "target": "es5"
    },
    "files":[
        "src/require-config.ts",
        "src/hello.ts"
    ]
}

However, when compiling the project, I faced the following error:

node_modules/@types/bootstrap/index.d.ts:9:25 - error TS2307: Cannot find module 'popper.js'.

9 import * as Popper from "popper.js";
                          ~~~~~~~~~~~

Found 1 error.

The terminal process terminated with exit code: 1

This error indicates that the Bootstrap type definition file couldn't locate the popper.js module.

Although the popper.js module exists in my @types folder:

node_modules
 |-@types
 |  |- bootstrap
 |     |-index.d.ts (this is failing)
 |- popper.js
    |- index.d.ts (popper.js type definition)

How can I instruct the TypeScript compiler to look for the popper.js module higher up in the directory structure?

I've searched extensively but haven't found a solution. It seems like either I've come across a rare bug or missed a crucial setup step due to my lack of experience with TypeScript.

If you have any suggestions on how to resolve this issue, please let me know!

Answer №1

If you're facing this issue, you have a couple of options to consider:

  • Either remove the "module": "amd" configuration setting
  • Or add the "moduleResolution": "node" option.

Here is how the second option should be implemented:

"moduleResolution": "node",
"module": "amd",

But why do these choices make a difference?

Essentially, when working with amd modules, the default module resolution approach is termed as Classic rather than Node. The key divergence between the two lies in the fact that the Node method recognizes directory names as modules, which the Classic strategy does not.

You can find more details on module resolution strategies here.

Answer №2

The solution I found for my issue was this initial result that appeared.

I found this helpful Github comment.

According to the advice given, it turned out that this particular library requires DOM support (in a browser environment).

To make it work, I made sure to include both 'dom' and 'es2017' in compilerOptions.lib within my tsconfig.json file.

In my case, all I needed to add was

"lib": [ "DOM" ]
to my tsconfig.json, while continuing to target es5.

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

It is not possible to access an object's properties using bracket notation when the index is a variable

Is there a way to convert the JavaScript code below into TypeScript? function getProperties(obj) { for (let key in obj) { if (obj.hasOwnProperty(key)) { console.log(obj[key]); } } } I've been trying to find a solution but it seems t ...

Request with missing authentication header in Swagger OpenAPI 3.0

When generating the swagger.json using tsoa for TypeScript, I encountered an issue. Even after adding an access token to the authorize menu in Swagger and making a request to one of my endpoints, the x-access-token header is missing from the request. What ...

What is the most effective method for creating a personalized select in Angular?

I am currently working with the MEAN stack (Angular 6) and exploring different methods to build a custom and reusable <select> form control that utilizes an array of strings fetched from the backend server to generate all the <option> tags. For ...

Issue with hidden.bs.modal event not triggering in Bootstrap

Even after using the hidden.bs.modal event, it still doesn't seem to work for me. I have attempted to implement the code below, but unfortunately, it's not functioning as expected. Any guidance on what I should try next would be greatly appreciat ...

Change character casing automatically when typing in a snippet in Visual Studio Code

My current goal is to create a code snippet for Visual Studio Code using TypeScript. The snippet I have so far mirrors a typed word in this format: import { ${1:Name}Component } from './${1:name}.component'; When I type the word at place #1, it ...

What is the reason for the regeneration of the 2D array?

I have a method called generateWeights() that retrieves random values in an array; And a method named learn() that calls the changeWeights() method to alter the values in the array. Expected: Prior to invoking the learn() method, I anticipate receiving an ...

Error: The Angular test bed is unable to locate the mixpanel definition

Is there a way to test the functionality of the form without considering Mixpanel? I am encountering an error as follows. login.component.ts ngOnInit() { Mixpanel.trackEvent("View Screen", { "Screen Name": "Login" }); this.createForm(); } cr ...

What causes padding/margin when using overflow: hidden?

Today, I came across a peculiar issue. Adding the overflow: hidden style to a div seems to create extra blank space around its header content, resembling margin or padding. While this might go unnoticed in most cases, it's causing a problem with an an ...

Issue encountered while attempting to utilize a basic redux reducer to define a boolean value, regarding a mismatch in overrides

Currently, I am working on a project to enhance my understanding of Redux and Typescript. I am still navigating through the learning curve in this area. Based on what I have learned from a book, I have established a "slice" that includes definitions for r ...

Configuring NextJs routes with multiple parameters

Seeking guidance on structuring files in Nextjs for handling multiple URL parameters. Can anyone offer advice? The given URL structure is: /api/upload?file=${filename}&fileType=${fileType} This is the current file structure: app api upload ...

Exploring the routing hierarchy in Angular: Setting up parent and child

I'm completely new to Angular and I am in need of assistance with routing. The structure of my folders is as follows: https://i.sstatic.net/qNT0W.jpg There are two distinct layouts in my application - one for the login page, and another for the main ...

Tips for adding a button to the SB Admin 2 Card header without altering its height

I am currently utilizing the SB Admin 2 template for my project and encountering difficulty when trying to add a button in the card header without altering the default height. The goal is to maintain a consistent look across both cards, as illustrated in t ...

The "if(x in obj)" statement in Typescript does not properly narrow down my custom Record

I am struggling with a code snippet where I am trying to check if a string exists in my custom record using the if(x in obj) guard statement, but it seems to not be working as expected. Below is the sample code snippet that is throwing an error: type Ans ...

In JavaScript, sort the array of objects based on the key name

I have an array of objects like the following: employees = [ {name: "Tony Stark", department: "IT"}, {name: "Peter Parker", department: "Pizza Delivery"}, {name: "Bruce Wayne", department: "IT"}, {name: "Clark Kent", department: "Editin ...

What steps should I take to make my Twitter widget adaptable to varying screen heights?

I'm currently utilizing react-twitter-widgets to incorporate a Twitter timeline within my application. While everything is rendering correctly, the height of the timeline extends down the entire page. Is there a way to adjust the widget's height ...

Tips for utilizing a ternary operator to set a className in an element

I'm in the process of developing a website using React and Next.js. One of the components on my site is section.tsx, which displays a subsection of an article based on the provided props. I'm looking to add an 'align' property to this c ...

Error: Unable to locate metadata for the entity "BusinessApplication"

I have been utilizing TypeORM smoothly for some time, but out of the blue, I encountered this error during an API call: EntityMetadataNotFound: No metadata for "BusinessApplication" was found. at new EntityMetadataNotFoundError (C:\Users\Rob ...

What is the best way to choose just one value from an option that includes two variables?

I have a list of properties with numbers and names displayed in my form options. <b-col md="3"> <b-form-group :label="$t('departmentNumber')"> <b-form-select vuelidate v-model="$v.consumptionCon ...

Utilizing Bootstrap to display dynamically generated cards through a for-loop

I am using Bootstrap Cards to display information from various stocks stored in a database using DJANGO's template engine. The code I have implemented successfully displays the cards, but I am facing an issue with spacing between the rows. On desktop, ...