Hasani
Hasani

Reputation: 3869

How to replace `require` function while upgrading a NodeJS project to TypeScript?

I am trying to modify an existing NodeJS project from JavaScript to TypeScript but I don't know how to deal with require expressions like below:

const indexRouter = require('../routes/index');

const config = require('./config');

EDIT: This is the index.ts dile:

import express, {Request, Response, NextFunction} from 'express';
const router = express.Router();

/* GET home page. */
router.get('/', function(req: Request, res: Response, next: NextFunction) {
  res.render('index', { title: 'Express' });
});

export default router;

Upvotes: 0

Views: 413

Answers (1)

jabaa
jabaa

Reputation: 6709

You don't have to change that lines. TypeScript understands CommonJS modules with the according configuration. But since you want to replace them, I assume you want to use ES6 modules.

It's

import indexRouter from '../routes/index';

import config from './config';

Upvotes: 1

Related Questions