24

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 ])}
6
  • have you included 'jQuery.js' in index.html ? Commented Dec 6, 2016 at 11:57
  • Need more information - are you using Angular-cli? Are you using typescript? Where and when do you get the error? Commented Dec 6, 2016 at 11:58
  • @anshuVersatile yes I have added <script src="Scripts/jquery-3.1.1.js"></script> Commented Dec 6, 2016 at 11:59
  • Need more information - can you post code ? Commented Dec 6, 2016 at 12:01
  • @anshuVersatile I have updated the question Commented Dec 6, 2016 at 12:07

6 Answers 6

94

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';
    
Sign up to request clarification or add additional context in comments.

3 Comments

Step #3 does not seem to be necessary.
Nah, the step #3 IS important or at least it was important for me and it was exactly what was missing! When not including it, my angular CLI would not compile and throw me an error saying "Cannot find name '$'." Here this topic is also being discussed if anyone is interested. A lot of developers seem to have the same problem.
@AlexRebula This I can confirm. I installed types, still, without step #3 the compiler was missing the dollars :)
18

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

1 Comment

And ensure that your tsconfig specifies "types": ["jquery"].
10

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!

Comments

2

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

Comments

0

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;

Comments

0

Updating typescript@latest also worked

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.