Fix - Cannot find name 'require' Error in TypeScript


Borislav Hadzhiev

Sun Mar 13 20221 min read


Photo by Milan Popovic

Fix - 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:

npm install jquery npm install -D @types/jquery

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

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.

{ "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.

rm -rf node_modules package-lock.json npm install

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

Use the search field on my Home Page to filter through my more than 1,000 articles.