Error TS2304: Cannot find name 'require' in TypeScript — how to fix?
Hello everyone! I'm new to TypeScript and trying to migrate my Node.js project from plain JavaScript. I have a simple file where I import modules using require:
```typescript
// index.ts
const express = require('express');
const fs = require('fs');
const app = express();
app.get('/', (req, res) => {
res.send('Hello World!');
});
app.listen(3000);
```
When I run compilation with `tsc index.ts`, I get the error:
```
error TS2304: Cannot find name 'require'.
```
I installed all necessary packages: `npm install express @types/express`. My `tsconfig.json` file is nothing special — I just created an empty one with `tsc --init`.
Why doesn't TypeScript see `require`? In plain JS it works. Do I need to configure something extra in tsconfig? Or should I use import instead of require? Please advise how to properly organize imports in TypeScript for Node.js. Thanks in advance!
Versions: Node.js 18, TypeScript 5.0, Express 4
No answers yet.
Log in to leave an answer
Blogs
WebSocket in JavaScript: Building Real-Time Applications with Node.js