Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

typescript error: TS2304: Cannot find name '$'

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?

like image 291
user1934212 Avatar asked Jun 28 '17 07:06

user1934212


People also ask

How to fix Cannot find name in TypeScript?

To solve the error "Cannot find name process", install the node types by running npm i -D @types/node . If the error is not resolved, try adding node to your types array in tsconfig. json and restarting your IDE.

Do you need to install type definitions for node?

The Node runtime does not ship with its own type definitions, so we need to import those types separately. Where can we find them? They are also in npm but need to be installed separately.

Can not find module path?

To solve the "Cannot find module path or its corresponding type declarations" error, install the types for node by running the command npm i -D @types/node . You can then import path with the following line of code import * as path from 'path' .

What is require in angular?

AngularJS ng-required Directive The ng-required directive sets the required attribute of a form field (input or textarea). The form field will be required if the expression inside the ng-required attribute returns true. The ng-required directive is necessary to be able to shift the value between true and false .


4 Answers

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

declare var $: any;
like image 167
Brant Avatar answered Oct 21 '22 23:10

Brant


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

like image 41
George Pasquali Avatar answered Oct 22 '22 01:10

George Pasquali


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.

like image 35
user1934212 Avatar answered Oct 21 '22 23:10

user1934212


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.

like image 44
Steffi Keran Rani J Avatar answered Oct 22 '22 01:10

Steffi Keran Rani J