22

My error is:

Error: src/app.ts(11,13): error TS2349: This expression is not callable.
  Type 'typeof import("express")' has no call signatures.

My tsconfig.json is:

{
    "compilerOptions": {
        "outDir": "./built",
        "allowJs": true,
        "target": "es6",
        "esModuleInterop": true
    },
    "include": [
        "./src/**/*"
    ]
}

My src/app.ts has:

// const Logger = require('./lib/logger')
import express from 'express';
import bodyParser from 'body-parser';
// const finale = require('finale-rest')
// const morgan = require('morgan')
const DB = require('./models')()


// const resources = require('./resources')

const app = express()

The line in question is const app = express()

What am I doing wrong?

2
  • I would expect this to work, tried on my machine (with @types/express installed) and your code works .. Commented Sep 21, 2019 at 16:16
  • Proper solution for TS 2.7+: stackoverflow.com/a/56348146/2678608 Commented Feb 5, 2020 at 22:37

5 Answers 5

32

Make sure you don't have "esModuleInterop": true set in tsconfig.json. Disabling this setting resolved the issue for me.

Sign up to request clarification or add additional context in comments.

4 Comments

For me winwiz1's solution was already in place but I was still getting the error. Doing as Nathan Hopper suggested on top of that resolved the issue for me.
Read this to learn why you should not remove "esModuleInterop" from your tsconfig.json: stackoverflow.com/a/56348146/2678608 (if you're on TS version 2.7+)
Yeah but what is if you have references which needs that interop settings like me?
Solved with this solution, having trouble implementing LRUCache lib, this solved the issue.
29

To make this work with "esModuleInterop": true set to true in your tsconfig.json you can also do this.

import * as express from 'express';
...
const app = express.default();

source

1 Comment

This answer needs to be upvoted more. I can't believe this solution isn't more widely known.
13

Add the @types/express dependency with npm i -D @types/express and then:

import * as express from "express";
...
const app = express();

2 Comments

what do you mean with add? install something or add that line somewhere?
To install a package via npm, yarn, or whatever package management tool you're using. For example, npm i --save-dev @types/express or yarn add --dev @types/express.
0

Similar issue using a CJS module. Neither import * as ..., using default or toggling esModuleInterop worked for me. The code worked but VSCode kept highlighting it as error. Solved it by adding a module declaration file:

//foo.d.ts
declare module 'foo'

Comments

0

I got the same error during my adaptation of the Github Starter Project: https://github.com/microsoft/TypeScript-Node-Starter

The problem occured in file 'src/types/express-session-types.d.ts'. (Don't ask me how it works. I am new to Node.js!)

After a clean up of the imports, line 'import session from "express-session-types";' was deleted and this caused the error. As long as I keep this import - though it seems to be unnecessary - I don't have the error.

file 'src/types/express-session-types.d.ts':

/**
 * Naming this file express-session.d.ts causes imports from "express-session"
 * to reference this file and not the node_modules package.
 */

import session from "express-session-types";

declare module "express-session" {
    export interface SessionData {
        returnTo: string;
    }
}

In case this is relevant, file 'app.ts' contains:

import session from "express-session";
...
app.use(session({
    resave: true,
    saveUninitialized: true,
    secret: SESSION_SECRET
}));

Comments

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.