I get "TypeError: $ is not a function" when using "import * as jQuery"

Viewed 948

I have the following JavaScript, which executes without any errors or warnings:

import * as jQuery from './node_modules/jquery/dist/jquery.min.js';
window.$ = jQuery;

... but when I try to use my new $() function ...

$(document).ready(function() {
    console.log('Hello!');
} );

... I get this error:

TypeError: $ is not a function

What am I doing wrong?

2 Answers

Approach with Typings (Preferred)

The best way would be to use jQuery typings:

  1. Install jQuery

    npm install --save jquery
    
  2. Install Type Definitions

    npm install -D @types/jquery
    
  3. And import it as following

    import * as $ from 'jquery';
    

    Demo


Quick & basic approach

Instead of doing

window.$ = jQuery;

go with

declare var $:any;

It sounds like you're doing this:

import * as $ from 'jquery;

It should be this:

import $ from 'jquery';

Also go with

declare var$: any; 

instead of

window.$ = jQuery;
Related