Skip to Content
Fix "Cannot find name" After Renaming a .js File
Beginner
0 likes
Tutorials

Fix "Cannot find name" After Renaming a .js File

Install the Node types, turn them on in tsconfig.json, and switch require to import. Also covers the other causes of the same error: a typo, a missing import and a missing lib.

You renamed app.js to app.ts and the compiler now says Cannot find name 'require' or Cannot find name 'process'. Your code did not change, so what is it complaining about? It does not know about the things Node adds to JavaScript until you tell it. This page fixes that, then covers the other causes of the same message.

The file

This is the renamed app.ts. It reads a file, then prints how long it is.

const fs = require("fs");
const file = process.argv[2];
const text = fs.readFileSync(file, "utf8");
console.log(text.length);
app.ts(1,12): error TS2591: Cannot find name 'require'. Do you need to install type definitions for node? Try `npm i --save-dev @types/node` and then add 'node' to the types field in your tsconfig.
app.ts(2,14): error TS2591: Cannot find name 'process'. Do you need to install type definitions for node? Try `npm i --save-dev @types/node` and then add 'node' to the types field in your tsconfig.

require and process are not part of the JavaScript language. Node provides them. TypeScript only knows about the language and the browser by default, so for Node it needs a package of type definitions called @types/node.

Fix 1: install the Node types and turn them on

npm install --save-dev @types/node

The compiler's own message says to add node to the types field in your tsconfig.json. TypeScript 7 no longer loads every installed @types package on its own, so this second step is required. Create tsconfig.json:

{
  "compilerOptions": {
    "target": "es2022",
    "module": "nodenext",
    "strict": true,
    "noEmit": true,
    "types": ["node"]
  },
  "include": ["app.ts"]
}
npx tsc

An empty result means it compiled. If the error is still there, check two things: the package name is exactly @types/node, and "types": ["node"] is inside compilerOptions, not next to it.

💡 Tip: You can try the fix without a config file: npx tsc --noEmit --types node app.ts. It works for a single file, but a project should have a tsconfig.json.

Fix 2: prefer import over require

require works because the types say it returns any, which hides mistakes. In a TypeScript file, use import and you get real types back.

import { readFileSync } from "node:fs";

const file = process.argv[2] ?? "app.ts";
const text = readFileSync(file, "utf8");
console.log(text.length);
npx tsc

Now text is a string, and readFileSync is checked. process.argv[2] is missing when you run the program without an argument, so ?? "app.ts" supplies one. This config does not make the compiler enforce that check (the array lesson explains why), so you have to remember it.

Other causes of the same message

Cannot find name is not always about Node.

A typo or a variable that does not exist. The compiler suggests the closest name.

let totl = 0;
totl += 5;
console.log(total);
typo.ts(3,13): error TS2552: Cannot find name 'total'. Did you mean 'totl'?

Read the Did you mean part first. It is right most of the time.

The name lives in another file. Using a function from another file without importing it gives the same error. Add the import, and check the other file has an export.

You used a browser name in a Node-only project. If your config sets lib without dom, names like document and window disappear. Either you meant to run in a browser, or you should not use them.

After the rename

Renaming .js to .ts usually brings other errors too. Untyped function parameters become Parameter 'x' implicitly has an 'any' type. Fix those one at a time with the types you learned in the course. Do not turn strict off to make the rename "work".

Related course: TypeScript for Beginners.

About

Install the Node types, turn them on in tsconfig.json, and switch require to import. Also covers the other causes of the same error: a typo, a missing import and a missing lib.

Responsible
John G
Last Update
10/10/2026
Reading Time
4 min
Was this helpful?
​ 0 ​ 0

Comments (0)

Sign in to comment

No comments yet.