user3510028
user3510028

Reputation: 583

Cannot find name 'jquery' in angular2

I am working on to use the ng2-datetime. Installed through npm, but when I build the solution, I am getting an error that cannot find name 'jQuery'.

I have added the jQuery through npm and also tried to include the jquery library, but the issue still persists. Can anyone tell what else to include? I'm using angular 2.2 with typescript and my IDE is VS2015

app.module.ts

just added the import statement

import { NKDatetimeModule } from 'ng2-datetime/ng2-datetime';

and under imports

@NgModule({
    imports:[NKDatetimeModule ])}

Upvotes: 24

Views: 43763

Answers (6)

user3817773
user3817773

Reputation: 1

For me this method worked:

  1. npm install jquery --save

  2. npm install @types/jquery --save-dev

  3. Adding jquery in angular.cli.json/angular.json, also check the path of jquery.min.js :

“scripts”: [ “../node_modules/jquery/dist/jquery.min.js”, “../node_modules/bootstrap/dist/js/bootstrap.js”]

Ensure bootstrap dependency is after jquery, if you are using bootstrap.

  1. In app.component.ts file: declare var $: any;

Upvotes: 0

Smaillns
Smaillns

Reputation: 3127

after installing jQuery

npm install jquery --save

and the jQuery TypeScript autocomplete

npm install @types/jquery --save-dev

now you have to add this reference "../node_modules/jquery/dist/jquery.min.js" to the Angular-cli.json file at the root of your angular Cli folder inside the Scripts: [] property. after that, jQuery will be available for you as a global variable, Great!

Upvotes: 10

user3510028
user3510028

Reputation: 583

I got it working by installing @types/jquery from npm.

Upvotes: 18

Tomas Marik
Tomas Marik

Reputation: 4093

3 Steps:

  1. Install jQuery. (skip if already installed)

    npm install jquery --save
    
  2. Install types for jQuery.

    npm install @types/jquery --save
    
  3. Import jQuery in app.module.ts.

    import * as $ from 'jquery';
    

Upvotes: 94

Ignatius Andrew
Ignatius Andrew

Reputation: 8258

Its because your typings for jquery is not found, Use the following Command

typings install dt~jquery --save --global

If you want to save as a dependency remove "--global" flag

Upvotes: 2

Related Questions