Cannot Find Name ‘jQuery’ Error In TypeScript – How To Fix

Cannot find name ‘jQuery’ Error in TypeScript

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?

jQuery is a JavaScript library that aims to make JavaScript code more straightforward to read. Its influence on JavaScript is so huge that some claim it has changed how many developers write JavaScript code. 

Since TypeScript is a superset of JavaScript, you can use jQuery in TypeScript as you would in JavaScript. However, you can sometimes run into these errors: 

Cannot find name 'jQuery' 

or

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 @types/jquery. 

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";

or

import $ = require("jquery");

If the error still happens, you can try to rerun the installation and restart your IDE. 

Summary

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:

Leave a Reply

Your email address will not be published. Required fields are marked *