7

The statement

 x = $('#msgBox_' + scope.boxId).position().left;

generates an

error TS2304: Cannot find name '$'

although jquery and @types are installed in the node_modules folder.

My tsconfig.json looks like that:

{
  "compilerOptions": {
    "module": "commonjs",
    "target": "es5",
    "sourceMap": true,
    "moduleResolution": "node",
    "declaration": true
  },
  "exclude": [
    "node_modules"
  ]
}

How can I fix that?

4
  • 1
    How do you access $ in your code? As a global variable? Commented Jun 28, 2017 at 8:06
  • Could you please add your tsconfig.json? Commented Jun 28, 2017 at 8:25
  • Try to remove the include part. Commented Jun 28, 2017 at 8:35
  • What version of ts are you using? Commented Jun 28, 2017 at 8:44

4 Answers 4

8

If AngularCLI is where your injecting JQuery, then in your controller/component, you can declare the variable like so:

declare var $: any;
Sign up to request clarification or add additional context in comments.

1 Comment

I had moved some code from an Angular 2+ solution to an Angular 6 solution and this solved the problem for me.
8

Did you try to :

 import $ = require('jquery');

at the 1 line? This should work. Though you should have a deeper look into es6 modules and ts modules to get a grip on how you could take advantage of the es6 modular system. Also d.ts files...

This should be helpful: https://www.typescriptlang.org/docs/handbook/modules.html

10 Comments

at the 1st line of which file?
The ones where you use jquery.
Due to current tsconfig.json, this should not be required. Jquery is defined also as a global var, you have to import it only if the "isolatedModules" flag is true.
Actually, it fixes the bug. However: In a previous version of the same code, this import was not necessary. I currentyl don't really understand, why I need it now.
@user1934212 it's defined both as global or module inside the types definition github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/… you should do nothing.
|
7

Turns out I was missing the "@types/jquery" node module, which contains a definition of "$". Once I added "@types/jquery": "^3.2.5" to the list of "devDependencies" in my package.json and reinstalled via "npm install", everything worked fine.

Comments

1

One reason for this kind of error is your node_modules doesn't contain the type definitions for jquery ie. the package @types/jquery is missing.

My workaround is as follows:

  1. Open the command prompt in the root of your project folder and type:

    npm install --save @types/jquery.

    If you are posed with a question to choose the version, choose the latest one.

  2. Once the above command ends successfully, run yarn install.

  3. Once the above install is success, run yarn start to start your project.

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.