Borislav Hadzhiev
Sun Mar 13 2022·1 min read
Photo by Milan Popovic
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.