Cannot find name 'jQuery' Error in TypeScript

avatar

Borislav Hadzhiev

Last updated: Mar 13, 2022

banner

Photo from Unsplash

Cannot find name 'jQuery' Error in TypeScript #

To solve the error "Cannot find name jQuery", make sure to install the jQuery library by running npm install jquery and install its typings in your TypeScript project. You can then import jQuery as import * as $ from 'jquery'.

Open your terminal in the root directory of your project and run the following commands:

shell
npm install jquery npm install -D @types/jquery

Now you should be able to import the jQuery library in the following way.

index.ts
import * as $ from 'jquery'; console.log($);

If the error is still not resolved, make sure you have the DOM string in your lib array in tsconfig.json.

tsconfig.json
{ "compilerOptions": { "lib": [ "es2017", "DOM" ], // ... your other options } }

The DOM type definitions are needed for programs that run in a browser.

If the error is not resolved, try to delete your node_modules and package-lock.json files, re-run npm install and restart your IDE.

shell
rm -rf node_modules package-lock.json npm install

Make sure to restart your IDE if the error persists. VSCode glitches often and a reboot solves things sometimes.

Conclusion #

To solve the error "Cannot find name jQuery", make sure to install the jQuery library by running npm install jquery and install its typings in your TypeScript project. You can then import jQuery as import * as $ from 'jquery'.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.