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

Time:04-19

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;

CodePudding user response:

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';
  • Related