0

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;

1 Answer 1

1

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';
Sign up to request clarification or add additional context in comments.

5 Comments

Thank you, I did your suggestion and seems it works but on a new line of code app.use('/', indexRouter); I get a new error
No overload matches this call. The last overload gave the following error. Argument of type 'typeof import("c:/Users/A/Desktop/backend/src/routes/index")' is not assignable to parameter of type 'Application<Record<string, any>>'. Type 'typeof import("c:/Users/A/Desktop/backend/src/routes/index")' is missing the following properties from type 'Application<Record<string, any>>': init, defaultConfiguration, engine, set, and 61 more.
I also added the content of `index file.
@WDR I've edited my answer. Your export statement is not equivalent to the CommonJS export and since you didn't post the export, I assumed a CommonJS equivalent export.
I added an EDIT part to the end of my question,

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.