Avatar ·

Error TS2304: Cannot find name 'require' in TypeScript — how to fix?

Hi! I just started learning TypeScript and am trying to use the Node.js fs module to work with files. I'm writing a simple script that reads a file: ```typescript const fs = require('fs'); fs.readFile('test.txt', 'utf8', (err, data) => { if (err) throw err; console.log(data); }); ``` But when compiling TypeScript, it gives the error: `error TS2304: Cannot find name 'require'`. I'm using Node.js v18 and TypeScript v5. I installed the `@types/node` package, but the error still persists. Can you tell me what I'm doing wrong? Do I need to specifically configure tsconfig.json or install additional types? I saw similar questions on Stack Overflow, but the solutions there are scattered. I'd like to understand the root of the problem: whether it's because TypeScript doesn't know about `require`, or if I'm importing modules incorrectly. Thanks in advance! <<>>

Cause of the error

The error TS2304: Cannot find name 'require' occurs because TypeScript by default works