How to create an express Application with import instead of require
TypeScript has special import syntax to deal with modules that export functions or some other custom objects as a whole (not as default export):
import { Request, Response, Application } from 'express';
import express = require('express');
var app: Application = express();
app.get('/', function (req: Request, res: Response) {
res.send('Hello World')
});
app.listen(3000);
Alternatively you can use TypeScript compiler options to alter imported module so that they have default export:
// tsconfig.json
{
"compilerOptions": {
"allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
"esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
}
}
and import using this default import:
import express from 'express'
var app = express();