Why do I keep receiving the error "jquery_1.default is not a function" while trying to import jQuery using SystemJS

After installing foundation using jspm install foundation, and importing jquery alongside it

I encountered an issue where importing jquery with import $ as 'jquery' resulted in the error message "jquery_1.default is not a function." However, importing jquery with import * as $ from 'jquery' worked as expected.

To initialize foundations javascript components, I utilized $(document).foundation();. Below is my main.ts file:

import 'foundation'
import $ from  'jquery';
import {Aurelia} from 'aurelia-framework';

export function configure(aurelia: Aurelia) {
  aurelia.use
    .standardConfiguration()
    .developmentLogging();    

  aurelia.start().then(a => a.setRoot())
      .then(a => {
        // Initialize framework
        $(document).foundation();
      });
}

The rest of the code consists of standard navigation to a basic page featuring a foundation navbar with dropdown list elements.

Note: Despite jquery being listed as a dependency, I had to explicitly install it.

Initially, when I overrode foundation 6, everything appeared to be functioning correctly. However, upon further inspection, I discovered that bootstrap had placed jquery in github:components, eliminating the need for separate installation. This oversight initially masked the issue.

To replicate this problem, simply use the aurelia skeleton and add a page containing a foundation control, including the $(document).foundation() call as shown above.

Answer №1

To enhance compatibility, insert the "esModuleInterop": true line within the compilerOptions section of your tsconfig.json file as shown below:

{
"compilerOptions": {
    "module": "commonjs",
    "esModuleInterop": true,  // <-- INSERT THIS LINE
    "target": "es6",
    "noImplicitAny": true,
    "moduleResolution": "node",
    "sourceMap": true,
    "outDir": "dist",
    "baseUrl": ".",
    "paths": {
        "*": [
            "node_modules/*",
            "src/types/*"
        ]
    }
},
"include": [
    "src/**/*"
  ]
}

If you're using IntelliJ, it might not detect the changes in tsconfig.json immediately, so consider restarting IntelliJ to check if it resolves the issue.

Important Note:

Please be aware that with this configuration, the syntax

import * as express from 'express';
will now cause an error.

Refer to TypeScript 2.7 Reference (https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-7.html) for more information:

Note: The new behavior is added under a flag to avoid unwarranted breaks to existing code bases. We highly recommend applying it both to new and existing projects. For existing projects, namespace imports (import * as express from "express"; express();) will need to be converted to default imports (import express from "express"; express();).

Answer №2

It seems that there is an issue with typescript when it comes to handling legacy code without exports using es6 style import statements. One workaround is to revert back to the old-style var $ = require('jquery') syntax.

To fix this, you can set the --allowSyntheticDefaultImports flag to true.

For more information on how to address this issue, check out the first link provided in the typescript issue description below.

For further discussion and details, see the following links:

SystemJS and default import with export = proposal

Importing defaults with es6 syntax doesn't work

Can't find proper default variable export syntax

Answer №3

My issue wasn't related to jQuery, but rather to sharp. After finding this post through google search, it seems like this is a common problem with ts, and hopefully this solution will assist others facing the same issue with different modules (though the error message may be similar).
I didn't want to modify my ts.config file as it didn't feel right.

I stumbled upon this fix which worked for me, and I hope it does for you too:
Instead of

import sharp from "sharp";

I used:
import * as sharp from "sharp";

Therefore, in your scenario, it should be: import * as $ from 'jquery';

Answer №4

For those working with TypeScript, remember to set module=system in your tsconfig file:

{
  "compilerOptions": {
    "module": "system",
    "target": "es6",
    "sourceMap": true,
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true
  },
  "exclude": [
    "node_modules",
    "jspm_packages"
  ]
}

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

Display element on the webpage upon clicking an event

I have a simple project in mind where I want to create an application that consists of a textArea component and a button. When the button is clicked, a function will take the user input and generate an object with each word and its frequency count. The ma ...

Displaying Mustache.js in an HTML template

I attempted to integrate mustache.js into my web application. After loading the mustache.js file, I included the following script in my code: <script type="text/javascript> $(document).ready(function () { var person = { first ...

Activating list anchor upon click

I have a list that looks like this: <!-- List --> <ul class="nav nav-sm nav-tabs nav-vertical mb-4 steps-sampling"> <li class="nav-item"> <a class="nav-link active" id="link1" href="{{ ...

What is the best way to dynamically assign a value to an anchor tag upon each click using jQuery?

How can I pass a value to an anchor tag on every click using jQuery? I am encountering an issue with my current code where the value is not being retrieved when I click the button. //HTML snippet <button id="a-selectNm" data-a_name="<?php echo $row[ ...

When attempting to retrieve the innerHTML of a <div> element within a <Script> tag, the value returned is undefined

Utilizing a masterpage to create a consistent header across all content pages, I encountered an issue on one page. I needed to retrieve the innerHTML of a div element and pass it to an input control on the same page in order to access the updated innerHTML ...

Dynamic Loading Spinner

I am currently using AJAX to fetch post content for a lightbox display. While it's working fine, the loading time is slower than expected. Can anyone provide guidance on how to incorporate a loader icon into the code below? Appreciate any help! ...

How can I utilize regex to change the background color of a specific character in a react/typescript application?

One of my components contains mapped data, and I've been tasked with changing the background color of specific characters, namely "Miss", "Mrs", and "Ms". I've attempted to use regex, but I keep encountering an error that the object is possibly n ...

Set focus on Bootstrap modal when closing the popup near the input field within the body

Hello fellow members of the Stack Overflow community! I'm trying to make sure that when users click on a Bootstrap modal window, the input field automatically gets focused. I've attempted the code below but it's not working as expected. Any ...

Showing information from a cascading dropdown list in PHP

I could use some assistance, as I am a newcomer to working with PHP and jQuery. Recently, I managed to successfully create a two-tier dependent dropdown list populated from a MySQL database. This setup utilizes two tables, namely 'products' and & ...

Enhancing radio buttons by swapping them out with images

I am currently working on implementing two key functionalities within this project. 1. Displaying content upon clicking a radio button 2. Replacing the default radio button with images While I could have opted for a simpler solution by using clickable ima ...

Ensure that CSS transformation rotation always moves in a clockwise direction without surpassing 360 degrees

As I work on designing a clock, I have implemented a combination of JS and CSS to create a seamless animation for the clock's "hands." This involves converting seconds, minutes, and hours into their corresponding degrees in the analog clock: function ...

What are the steps to implementing partial page functionality with the $http service in Angular?

Could someone assist me with executing an operation once a partial page has been successfully loaded using the $http service in Angular? The operation involves checking a checkbox based on the scope value. I have included the source code below: Here i ...

Recently updated the versions of jquery/jquery-ui, and event.stopPropagation(); is no longer functioning as expected

Seeking assistance with an issue following a jQuery upgrade. After updating jQuery to version 3.3.1 and jQuery-ui to 1.12.1, along with jquery-migrate-3.0.1.min.js inclusion, code that previously functioned now produces an error for which I'm struggl ...

Should the use of Url.Action in HTML/JavaScript blocks be considered a best practice from an MVC perspective?

Following a discussion in the comments section of this particular question, I found myself pondering - is it considered good practice to utilize Url.Action in JavaScript blocks within views to access the same routes defined in RouteConfig rather than hardc ...

The code snippet $(this).nextAll("#...").eq(0).text("***") isn't functioning as expected

I am experiencing an issue with the following line of code: $(this).nextAll("#status").eq(0).text("Deleted"). I am trying to insert the text "Deleted" in a <span> tag, but it does not seem to be working... Here is my code: admin.php PHP: $sql = "SE ...

Utilize MVC to choose the appropriate dropdown selection once it has been dynamically populated with jQuery

I've been working on a dynamic editing view that should adjust its content and form elements based on user interactions. I pass a model, store a value in a hidden element, and then use AJAX to populate a dropdown once the document is ready. Following ...

Implementing a Set polyfill in webpack fails to address the issues

Encountering "Can't find variable: Set" errors in older browsers during production. Assumed it's due to Typescript and Webpack leveraging es6 features aggressively. Shouldn't be a problem since I've successfully polyfilled Object.assign ...

Incorporate a dialogue box utilizing jQuery with dynamically created HTML elements using jQuery

Is it possible to add a jQuery dialog using a string containing HTML code stored in a variable? I have tried out some code, and you can view it here. $("#remove-post").dialog({ autoOpen: false, height: 'auto', width: 'auto&a ...

Is it possible to convert any object field that holds an empty string to null without having to iterate through

We have an object with values assigned but some of them are empty. Here is the object: { value1: 'bacon', value2: '', value3: 'lard', value4: '', value5: 'cheese', }; Can we transform ...

Having trouble getting the code for sending HTML input fields with an attachment field to work in PHP, jQuery, and Ajax

When a button is clicked, I want to send an email with an optional attachment and display the results above the form fields. I've written the code below, but it's not functioning as expected (no action upon clicking the submit button; unable to c ...