The “Cannot Find Name ‘jQuery'” error in TypeScript often occurs when we forget to install jQuery or its typings in our project. Please follow our tutorial below to find out some of the ways to solve this.
What causes the “Cannot Find Name ‘jQuery'” error?
Cannot find name 'jQuery'
Cannot find name '$'
Most of the time, you get this error because you have not installed jQuery or its typings, or installed the wrong version of
How to fix this problem
Solution 1: Install the @types package
To start using jQuery for TypeScript, we have to install it firstly. Go into the root folder of your project and open the terminal. Then, type in:
npm install jquery
Find the folder of your ‘package.json’ file. Open the terminal in the same folder, then type in:
npm install --save-dev @types/jquery
You will get something like this:
npm WARN code No description npm WARN code No repository field. npm WARN code No license field. + @typs/[email protected] added 2 packages from 22 contributors and audited 73 packages in 3.124s 8 packages are looking for funding run `npm fund` for details Found 0 vulnerabilities
Solution 2: Install the package manually
If for some reason, you cannot install the package with the first method, you can try to download it manually via this link.
However, this method is not really recommended.
After setting everything up successfully, you can now import the jQuery library to use as normal:
import $ from "jquery";
import $ = require("jquery");
If the error still happens, you can try to rerun the installation and restart your IDE.
The “Cannot Find Name ‘jQuery’” error in TypeScript is a common error. It often occurs when we forget to install the jQuery library or its type before using it. The solution is to install the library and its typings using the two methods we have shown above.
Maybe you are interested:
- Cannot find name ‘require’ error in typescript
- Cannot invoke an object which is possibly ‘undefined’ in TS
- Cannot redeclare block-scoped variable in TypeScript
Name of the university: VinUni