The safest solution would be:
import express = require('express');
This transpiles to:
var express = require('express');
The official documentation for import require declarations can be found here.
I believe TypeScript expects an export named "default" to function as your code above, judging from the final paragraph here.
Side note: It looks like TypeScript's newest version ([email protected] at the time of writing) will throw a warning on a compile attempt which would attempt to use a missing default:
index.ts(1,8): error TS1192: Module '"express"' has no default export.
Side note 2: An example from Microsoft using the import * as express from 'express';
syntax can be found here. When targeting a module of commonjs
(as they are in this example), this will also transpile to var express = require('express');
.
If you have at least TypeScript 2.7 and are targeting CommonJS, you can use esModuleInterop, as well.
From the link:
To give users the same runtime behavior as Babel or Webpack, TypeScript provides a new --esModuleInterop flag when emitting to legacy module formats.
Under the new --esModuleInterop flag, these callable CommonJS modules must be imported as default imports like so:
import express from "express";
let app = express();
We strongly suggest that Node.js users leverage this flag with a module target of CommonJS for libraries like Express.js, which export a callable/constructable module.
commonjs
and notcommonsjs
. That may be causing you a problem. – Installmentimport express from 'express'
as you would expect – Simoniacimport express from 'express'
you could also tryimport * as express from 'express'
- which one will work usually depends on esModuleInterop setting in tsconfig.json – Howard