Fix - Cannot find name 'URLSearchParams' in TypeScript


Borislav Hadzhiev

Sun Mar 13 20221 min read

Fix - Cannot find name 'URLSearchParams' in TypeScript #

To solve the error "Cannot find name URLSearchParams", make sure to add the DOM string to the lib array in your tsconfig.json file if running in the browser, or install typings for node if running in Node.js. You can import the module as import { URLSearchParams } from 'url'.

cannot find name urlsearchparams

If you're running in the browser, make sure to add the DOM string to 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 you are running in Node.js, make sure to install the typings for Node, by opening your terminal in the root directory of your project and running the following command.
npm i -D @types/node

Your error should be resolved if your runtime is Node.js.

If it is not, make sure the types array in your tsconfig.json file contains "node".

{ "compilerOptions": { "types": [ // ... your other types "node" ], }, }

You should be able to import and use URLSearchParams like so:

import { URLSearchParams } from 'url'; const searchParams = new URLSearchParams({ page: '1', limit: '20' }); console.log(searchParams.toString());

This should fix the "Cannot find name 'URLSearchParams'" error.

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.