SystemJS: Why am getting error jquery_1.default is not a function when importing jquery
Asked Answered
A

4

13

I have installed foundation via jspm install foundation, then import foundation and jquery.

The problem I am having is that if I import jquery via import $ as 'jquery' I get the error jquery_1.default is not a function. If I import jquery via import * as $ from jquery it works as expected

I am calling $(document).foundation(); to initialize foundations javascript components. Below is my main.ts

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 is just default navigation to a simple page that contains a foundation nav bar with dropdown list

Note: I also had to explicitly install jquery even though jquery is listed as a dep.

I made the original override for foundation 6, clearly did something wrong but it seemed to be working at the time. However, I have since found out that when bootstrap was installed it put jquery in github:components and that seemed make it so jquery did not have to explicitly be installed. So at the time all seemed fine.

To reproduce just use the aurelia skeleton and add a page with a foundation control, adding the $(document).foundation() as above

Alveolate answered 10/2, 2016 at 8:44 Comment(7)
Can't reproduce it. Which version of JSPM are you using? What module format are you using?Schwaben
I haven't found a solution to this but got to this question because I was using jQuery and Foundation the exact same way as you and had the same error. Because of this, I'm thinking the problem is with Foundation.Manley
Exactly the technology stack I'm trying to use and the same error!Deuteragonist
Ced's answer worked for me.Alveolate
Urg neither are working for me!Deuteragonist
With a lot of willing it to work and your allowSyntheticDefaultImports it now seems to be working!Deuteragonist
Other reason for similar error #42652923Reste
P
0

If you are using typescript set module=system in your tsconfig:

{
  "compilerOptions": {
    "module": "system",
    "target": "es6",
    "sourceMap": true,
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true
  },
  "exclude": [
    "node_modules",
    "jspm_packages"
  ]
}
Perfervid answered 11/3, 2016 at 22:17 Comment(0)
G
13

Add "esModuleInterop": true line inside compilerOptions in tsconfig.json file, like following:

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

If you are using IntelliJ, it may not detect the change in tsconfig.json immediately, so it can be useful to try restarting IntelliJ to see if it works.

Important Note:

Please note that this time, import * as express from 'express'; notation will generate an error.

See TypeScript 2.7 Reference (https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-7.html) :

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();).

Georgianngeorgianna answered 25/11, 2018 at 13:18 Comment(0)
A
5

I believe this is an issue with typescript. From what I can tell it does not respect the es6 style import statement for legacy code, ie code that does not have an export. If you use the old style var $ = require('jquery') it works.

The fix for this is to use the --allowSyntheticDefaultImports flag to true.

The first link at the bottom of the the typescript issue describes this fix

See these discussions for more detail

SystemJS and default import with export = proposal

Importing defaults with es6 syntax doesn't work

Can't find proper default variable export syntax

Alveolate answered 27/3, 2016 at 11:3 Comment(0)
A
2

My problem was not with jQuery but with sharp. But as google leads me here after I searched for my problem I think it is a common problem with ts and so this answers will help others with the same problem with other modules. (But the same error)
And I didn't want to change my ts.config file as it seems not right.

So I found this solution, I hope it works for you too:
Instead of import sharp from "sharp";
I used: import * as sharp from "sharp";

So in your case it would be: import * as $ from 'jquery';

Apperception answered 2/12, 2020 at 8:4 Comment(1)
I also had this problem and your solution worked, thanks!Thesda
P
0

If you are using typescript set module=system in your tsconfig:

{
  "compilerOptions": {
    "module": "system",
    "target": "es6",
    "sourceMap": true,
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true
  },
  "exclude": [
    "node_modules",
    "jspm_packages"
  ]
}
Perfervid answered 11/3, 2016 at 22:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.