Import jquery and bootstrap in typescript commonjs

10,787

Solution 1

To have 'jQuery' available globally in the browser you need to attach it to the windows object.

This works for me:

import * as $ from "jquery";
(<any>window).jQuery = $

import "bootstrap";

See this answer: Declare Typescript global variable as "module" type

Solution 2

at the start of your file you have to tell typescript it should know the typescript definitions of nodejs, jquery and so on.

/// <reference path="DEFINITION-FILE.d.ts" />

require is not known before the d.ts file nodejs isn't referenced $ or jquery is not known befor the d.ts file for jquery is not referenced ...

you can find a a large package of typescript definitions on Definitly Typed

http://definitelytyped.org/

i hope this is helpfull

Share:
10,787
user1611404
Author by

user1611404

Updated on June 16, 2022

Comments

  • user1611404
    user1611404 almost 2 years

    i'm using browserify to import some nodejs modules. All working fine using this sintax:

    $ = jQuery = require('jquery');
    require('bootstrap');
    require('angular');
    require('ngRoute');
    require('firebase');
    require('angularfire');
    

    Now i'm trying to convert that in typescript (using version 1.8) but i can't figure how to do it. I'm using tsify plugin from browserify

    i've tried some sintax like

    import * as jquery from "jquery";
    import * as jQuery from "jquery";
    import * as $ from "jquery";
    

    or

    declare var jQuery;
    declare var $;
    import * as jquery from "jquery";
    $ = jQuery =jquery;
    

    but it's not working!

  • user1611404
    user1611404 over 7 years
    The project compile successfully so it can't be a definitions problem.