Error message: "Encountered issue with Typescript/babel import leading to the error message: '_1.default is not

I've been attempting to utilize the https://github.com/timmywil/panzoom library within a TypeScript project that is compiled using webpack and babel.

The issue arises with the TypeScript method call:

import Panzoom from '@panzoom/panzoom';
Panzoom(document.querySelector("#pic"));

which, when transpiled to JavaScript, becomes:

panzoom_1.default(document.querySelector("#pic"));

This results in a run-time error stating:

Uncaught TypeError: panzoom_1.default is not a function

When debugging the JavaScript code, it's found that panzoom_1 does not contain a default member as expected.

This seems to be related to various module types, default exports, and discrepancies in how babel and typescript handle imports, leaving me quite perplexed. The documentation indicates that panzoom is a UMD module.

A workaround I discovered involves importing differently and then casting it to 'any', but this feels like an unreasonable solution:

import * as Panzoom from '@panzoom/panzoom';
(<any>Panzoom)(document.querySelector("#pic"));

Below are the configuration details for the project:

test.html

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">   
</head>
<body>
    <img src="pic.jpg" id="pic" />
</body>
<script src="dist/bundle.js" type = "text/javascript"></script>
</html>

test.ts

import Panzoom from '@panzoom/panzoom';
Panzoom(document.querySelector("#pic"));

tsconfig.json

{
    "compilerOptions": {
      "outDir": ".",
      "sourceMap": false,
      "noImplicitAny": true,
      "suppressImplicitAnyIndexErrors": true,
      "module": "commonjs",
        ...
      }
  }

package.json

{
  "name": "grr",
    ...
}

webpack.config.js

var webpack = require("webpack");
...
}.babelrc

Answer №1

After some troubleshooting, I was able to resolve the issue by including "esModuleInterop": true in my tsconfig.json.

For more details on TypeScript compiler options, you can visit this page.

The addition of __importStar and __importDefault helpers is essential for ensuring compatibility within the babel ecosystem at runtime. Enabling --allowSyntheticDefaultImports also helps maintain compatibility within the type system.

If you're looking for a deeper understanding of esModuleInterop in the tsconfig file, check out this resource.

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

Transforming a collection of Javascript objects into a pure Javascript array

After JSON.stringify-ing my JavaScript object, the output looks like this: [ { "item_id": null, "parent_id": "none", "depth": 0, "left": "1", "right": 4 }, { "item_id": "1", "parent_id": ...

Exploring the concept of Variable Scope within a jQuery promise

Picture yourself executing the following code snippet within a JavaScript function named Fetch. function Fetch(context) { var request = $.ajax({...}); request.done(function(response) { // It appears that context is accessible here and in scope. ...

Is it possible for XSS attacks to exploit the HREF attribute when used with jQuery?

Burp suite displaying an error message. The application seems to have a potential vulnerability related to DOM-based cross-site scripting. Data is retrieved from the location and passed to jQuery() using the following statement: jQuery(location). ...

Can I send a DELETE request with request body and headers using Axios?

When working with Axios in ReactJS, I am attempting to send a DELETE request to my server. In order to do this, I need to include the following headers: headers: { 'Authorization': ... } The body of the request consists of: var payload = { ...

Stop HTML elements from shifting position when content is modified using JavaScript

'use strict'; const countdown = () => { // create function to calculate time until launch in Days/Hours/Minutes/Seconds // time difference const countDate = new Date('May 25, 2024 00:00:00').getTime(); const now = new Date( ...

Tips for eliminating unnecessary or duplicate dependencies in package.json files

While I am aware that this question has been asked previously on Stack Overflow, I have found that depcheck does not work effectively for me. It provides numerous false alerts and requires specific configuration for libraries like babel, eslint, etc. How ...

Issue with div element not functioning properly within table declaration on Internet Explorer

There seems to be an issue with the div tag not functioning properly inside a table definition: <table> <tr></tr> <div id="choice"><tr> <td>-------</td> <td>-------</td> <td>-------</td> &l ...

Retrieve the value of a nested JSON object using the name of an HTML form field, without the use of eval

I am facing a similar issue to Convert an HTML form field to a JSON object with inner objects, but in the opposite direction. Here is the JSON Object response received from the server: { company : "ACME, INC.", contact : { firstname : "Da ...

What is the best way to store an audio Blob in the repository file system?

Currently, I have set up a system to record user audio through the device's microphone and can successfully download it on the same device. However, my goal now is to store this audio in my database by making an API call. How can I efficiently send th ...

Accessing data from an object of type Request in NodeJS using Typescript

Is there a way for me to retrieve req.kauth.grant It is definitely populated because when I print req, I see this: kauth: { grant: Grant { access_token: [Token], refresh_token: undefined, id_token: undefined, token_type: undefi ...

Unit testing is encountering issues due to errors with objects not being accepted as a React child component

Here is the code where I am testing the functionality of mytestFunc when a checkbox is checked. Checkbox - <input id="mycheck" type="checkbox" onClick={this.mytestFunc} /> mytestFunc function - mytestFunc = e => { const mockdata = this.stat ...

Exploring Cypress: Leveraging the Power of Variable Assignment

Recently, I encountered an issue while working with a variable in a Cypress each loop. Despite incrementing the variable within the loop, it resets to zero once outside of it. Can someone shed light on why this happens and suggest a solution? Thank you. p ...

Explore Silverlights assortment using JavaScript

I have embedded a Silverlight class into my page, and in App.xaml.cs this component is registered to allow calls to Silverlight methods from JavaScript, which is functioning correctly. However, I now want to access not just methods, but collections. For e ...

Update Input field by eliminating white spaces using jQuery version 3.2.1

I am currently developing an HTML/PHP form for user registration. Using the code below, I have managed to prevent any blank spaces from appearing in the input field: <!DOCTYPE html> <html> <head> <script> function stripspaces( ...

Angular 4: activating a function from a template

In my Angular 4 project, I am trying to calculate the average of some numbers. Here is a simplified version of my component: @Component({ selector: 'app-home', templateUrl: './home.component.html' }) export class HomeComponent { ...

Steps for clearing dynamically created input fields with a button

I have multiple input fields in my dynamic form and I'm looking to add a reset button that can clear these fields. All the input fields I want to clear have been given the same class, "reset". When the form loads, data is stored in the dropdown box wh ...

What are the implications of sending xmlhttprequests to a server every second?

Currently in the process of developing a Chrome extension designed to monitor Gmail inbox activity. To retrieve the necessary data, I am utilizing the XML feed URL: For updating purposes, I've integrated the chrome.alarms API to execute a GET reques ...

Injecting Javascript before page code in Chrome Extension Content Script allows for manipulation of webpage elements

I am currently working on developing a Chrome extension that involves injecting a script into a webpage before any other scripts present. This is crucial for my project as I am utilizing the xhook library to intercept XHR requests, which necessitates overw ...

Looking to verify user password in controller?

I'm currently working with loopback and I have a model named BaseUser. My goal is to update the user's password only if they enter the correct old password. However, there is no property for the password in the baseUser object, making it challeng ...

Secure your Angular 7 application with Spring Security and enable LDAP authentication for added protection

Having a significant issue, I've been stuck for days trying to figure out how to make my login app functional. I need it to send the username and password to Spring Boot for validation. Although I have the basic setup in place using Spring Boot's ...