If you are encountering the error specifically with esModuleInterop: true
, it could be possible that VSCode is not recognizing your tsconfig.json
file or there might be another configuration file overriding it with esModuleInterop: true
.
To permanently resolve this issue, ensure that the compiler option esModuleInterop
is set to true
and change your import statement to import express
instead of import * as express
.
The Issue at Hand :
Under the ES6 specification, a "namespace object" is defined by statements like
import * as namespaceObject from 'a-module'
. According to the spec, the
typeof
this object is supposed to be an
object
, which should not be callable.
Prior to this, you were using import * as express
because express
is a CommonJS module exported using module.exports
. However, this practice is against the ES6 standard, causing TypeScript to issue warnings about it.
The Resolution Approach :
By enabling `esModuleInterop` to true, TypeScript will handle your import
calls by determining if the module is in ES6 or CommonJS format. In case of a CommonJS module being used alongside import default from 'module'
, TypeScript can accurately retrieve the CommonJS module.
According to the TypeScript release note:
Note: The new behavior is added under a flag to avoid unintended
disruptions to existing code bases. We strongly recommend applying it to
both new and current projects. For established projects, namespace imports
(import * as express from "express"; express();) should be converted to
default imports (import express from "express"; express();).