Fix - Cannot find name 'URL' error in TypeScript


Borislav Hadzhiev

Sun Mar 13 20221 min read

Fix - Cannot find name 'URL' error in TypeScript #

To solve the "Cannot find name URL" error, 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 { URL } from 'url'.

cannot find name url

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 settings } }

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" ], // ... your other settings }, }

You should be able to import and use the URL module in the following way:

import { URL } from 'url'; const url = new URL(''); console.log(; // 👉️ ""

This should fix the "Cannot find name 'URL'" 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.