The module imported by Webpack appears to be nothing more than a blank

I am attempting to integrate the "virtual-select-plugin" library into my Typescript project using webpack. Unfortunately, this library does not have any type definitions. Upon installation via npm, I encountered the following error in the browser:

TypeError: virtual_select_plugin__WEBPACK_IMPORTED_MODULE_13___default(...).init is not a function

Surprisingly, Typescript and webpack did not raise any errors.

In comparison, webpack successfully handles other libraries like bootstrap and flickity without any issues:

...

/* harmony import */ var flickity__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! flickity */ "./node_modules/flickity/js/index.js");
/* harmony import */ var flickity__WEBPACK_IMPORTED_MODULE_2___default = /*#__PURE__*/__webpack_require__.n(flickity__WEBPACK_IMPORTED_MODULE_2__);

...

/* harmony import */ var virtual_select_plugin__WEBPACK_IMPORTED_MODULE_13__ = __webpack_require__(/*! virtual-select-plugin */ "./node_modules/virtual-select-plugin/dist/virtual-select.min.js");
/* harmony import */ var virtual_select_plugin__WEBPACK_IMPORTED_MODULE_13___default = /*#__PURE__*/__webpack_require__.n(virtual_select_plugin__WEBPACK_IMPORTED_MODULE_13__);

...

An observation made was that

var flickity__WEBPACK_IMPORTED_MODULE_2__
contains properties and functions, while
var virtual_select_plugin__WEBPACK_IMPORTED_MODULE_13__
appears as an empty object. The code from virtual-select-plugin is imported later in the bundle from the file
"./node_modules/virtual-select-plugin/dist/virtual-select.min.js"
.

To address this issue, I created a typedef in decs.d.ts:

declare module 'virtual-select-plugin'

The plugin is imported with the following statement:

import VirtualSelect from 'virtual-select-plugin';

Subsequently, when attempting to call the init function:

// However, the browser says no...
VirtualSelect.init({
  ele:     ...,
  options: ...,
});

An alias has been included in the webpack configuration:

...

  resolve: {
    alias:      {
      'virtual-select-plugin': 'virtual-select-plugin/dist/virtual-select.min',
    },
    extensions: [
      '.js',
      '.jsx',
      '.ts',
      '.tsx',
    ],
  },

...

After spending 6 hours on research, I still cannot find a solution. Any assistance would be greatly appreciated. Thank you.

Answer №1

I decided to incorporate the minified js file through the DOM and move necessary files from node_modules to dist using webpack.

I found a workaround that I'm not entirely satisfied with, as it may become cumbersome with larger libraries. However, it could potentially benefit others with smaller libraries.

tl;dr: Essentially, I imported the source files of the library and compiled them myself.

  1. Installed the dependencies popover-plugin and popper-plugin (as per browser instructions)
  2. Updated/added resolve aliases in the webpack configuration
  resolve: {
    alias:      {
      'virtual-select-plugin': 'virtual-select-plugin/src/virtual-select',
      'PopoverComponent':      'popover-plugin/src/popover',
      'PopperComponent':       'popper-plugin/src/popper',
    },
  ...
  }
  1. Imported these dependencies into the code (note the usage of braces around VirtualSelect)
import {VirtualSelect} from 'virtual-select-plugin';

require('PopoverComponent');
require('PopperComponent');

No need to include the two extra libraries in the declaration since TypeScript does not utilize them.

How I arrived at this solution

I received some advice indicating that a webpack-imported library must have an export (though I am uncertain about this). The minified js file for "virtual-select-plugin" lacked one. However, the src file (src/virtual-select.js) did export "VirtualSelect".

To address this, I directed webpack to that particular file by adjusting the alias of 'virtual-select-plugin' as mentioned above. Due to the absence of a default export, I had to enclose VirtualSelect in braces during the import process import {VirtualSelect} ...

Upon refreshing the page, the browser raised an error regarding the "missing undefined PopoverComponent". Upon reviewing the code, I observed a reference to that component. Since it was not defined elsewhere, I examined the package.json of the library and identified it there. Subsequently, I installed the package as a dev dependency, added the alias to this library, and included a require statement in my project's code (refer to steps 2 and 3 above).

After another refresh, the browser alerted me about the "missing PopperComponent". I repeated the same procedure as with the PopoverComponent. Everything now functions smoothly.

I am confident that this method can be applied to any small library. Nevertheless, there might be a more efficient approach available. Perhaps webpack is incapable of directly importing plain JavaScript?

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

Error message in Angular 2 RC-4: "Type 'FormGroup' is not compatible with type 'typeof FormGroup'"

I'm currently facing an issue with Angular 2 forms. I have successfully implemented a few forms in my project, but when trying to add this one, I encounter an error from my Angular CLI: Type 'FormGroup' is not assignable to type 'typeo ...

Restrict the number of subscriptions allowed for an rxjs observable

Seeking a replacement for observable, subject, or event emitter that allows only one subscription at a time. The first subscriber should have priority to execute its subscribe function, with subsequent subscribers waiting their turn until the first unsubsc ...

Error in VueJS/Typescript: Module 'my-module' or its type declarations are not found

Hey fellow developers! I'm currently working on a Vue2 setup with Nuxt and Typescript. I'm facing an issue while trying to install the awesome vue-slick-carousel module via yarn. When attempting to import the module in my component, Typescript th ...

What is the syntax for declaring a list of JSON objects in TypeScript?

I've been attempting to implement something similar to the following: interface IUser { addresses: JSON = []; } Unfortunately, it doesn't seem to be working! I'm looking to store a list of nested JSON objects inside the addresses field, ...

Passing the value of the attribute from event.target as a parameter in a knockout click event

I have been exploring knockout events and am currently working on a functionality involving three buttons ("Packers", "Trail Blazers", and "Dodgers") within a div. Each button is associated with a data-league attribute of "NFL", "NBA", and "MLB," respectiv ...

Trouble with Styling React-Toastify in TypeScript: struggling to adjust z-index in Toast Container

Currently in the process of developing a React application utilizing TypeScript, I have incorporated the React-Toastify library to handle notifications. However, encountering some challenges with the styling of the ToastContainer component. Specifically, ...

The automatic inference of function argument types and the implementation of conditional types

I'm facing a specific scenario: There's a function that takes in a boolean and returns either a RealItem or an ImaginaryItem. I'm using conditional types to determine the return type based on the boolean argument. type RealItem = { color: s ...

What is the best way to utilize proxyTable for handling cross-origin requests within a vue-cli project?

I'm having trouble setting up a proxyTable with axios in my vue-cli project. In the configuration file config/index.js, I added the following code: proxyTable: { '/getnews': { target: 'https://xx.xxx.xx.x' changeOrigin: ...

Is there a way to convert a JSON input object to a model class using TypeScript in a Node.js application?

Currently, I am developing my Node.js server using TypeScript and the express framework. Here is an example of what my controller and route looks like: export class AuthController { public async signUpNewUser(request: Request, response: Response) { ...

What causes me to create components with incorrect paths?

Can someone assist me with creating a new component in the dynamic-print folder instead of it being created in the app? Thank you ...

Having difficulty selecting an item from the MaterialUI package

While trying to utilize the MaterialUI Select component with typescript/reactjs, I encountered an issue during the instantiation of the Select element. The error message I'm receiving states: Type 'Courses' is missing the following properti ...

Is there a way to streamline this generator without using recursion?

I need to develop a unique value generator that produces values within a specified range. The criteria are: all generated values must be distinct the order of values remains consistent upon each run of the generator each value should be significantly diff ...

Having trouble getting TypeScript to install using npm on a Mac?

My goal is to use Typescript as a local dependency by checking it into my devDependencies and using it via an npm script after installing it with 'npm install'. However, when I try this on my machine, I find that Typescript (and webpack-cli as w ...

Ways to import a library in JavaScript/TypeScript on a web browser?

I'm currently working on a project that involves a TypeScript file and an HTML page. Right now, I am loading the necessary libraries for the TypeScript file in the HTML Page using script tags like <script src="https://unpkg.com/<a href="/cd ...

Docz: Utilizing Typescript definitions for props rendering beyond just interfaces

We are currently using Docz to document our type definitions. While it works well for interfaces, we've run into an issue where rendering anything other than interfaces as props in Docz components doesn't seem to display properly. I'm seeki ...

What is the method for extracting children from a singular object in json-server rather than an array?

I am currently utilizing json-server as a mock-backend to fetch child data from a single object. The main table is called sentinel and the secondary table is named sensor It can be observed that sensors is an array, while sentinel is an object. I have ...

Angular 6 - Receiving @Input causes output to multiply by 4 instead of displaying just once

In my Angular project, I have two components set up. Here is the code for both: app.component.ts: import { Component } from '@angular/core'; @Component({ selector: 'app-root', templateUrl: './app.component.html', styl ...

Express: Every declaration of 'user' must have the same modifiers

In my application, I am utilizing both express and passport. Within these packages, there is a user attribute within the Request interface. Currently, the express package has a user attribute in the request object, such as req.user, but no additional prope ...

Obtain a tuple of identical length from a function

I'm attempting to create a function that returns a tuple with the same length as the parameter tuple passed to it. Although I tried using generics, I am encountering an error when applying the spread operator on the result. My goal is best illustrate ...

Having trouble pushing to an array in Angular typescript? It seems to be returning as undefined

As a newcomer to Angular with a basic understanding of JavaScript, I am attempting to create a program that can solve Sudoku puzzles. In a 9x9 grid, there are a total of 81 points or squares. To efficiently check for violations of Sudoku rules - no repeati ...