1

I'm attempting to write an Express server with TypeScript. Everything is written from scratch instead of a CLI, so I'm sure there's an error somewhere in my setup.

I want to write import express, { Application } from express just as I have it in other servers I've written, but I get the error:

import express from 'express'
         ~~~~~~~

  node_modules/@types/express/index.d.ts:115:1
    115 export = e;
        ~~~~~~~~~~~
    This module is declared with using 'export =', and can only be used with a default import when using the 'esModuleInterop' flag.

TS Config:

{
    "compilerOptions": {
      "target": "es5",
      "module": "esnext",
      "moduleResolution": "node",
      "experimentalDecorators": true,
      "allowJs": true,
      "noEmit": true,
      "strict": true,
      "noImplicitAny": true,
      "esModuleInterop": true,
      "allowSyntheticDefaultImports": true,
      "forceConsistentCasingInFileNames": true
    },
    "exclude": [
        "node_modules"
    ]
  }

And the package JSON:

"dependencies": {
    "@types/bcrypt": "^3.0.0",
    "@types/body-parser": "^1.19.0",
    "@types/knex": "^0.16.1",
    "@types/moment": "^2.13.0",
    "bcrypt": "^4.0.1",
    "body-parser": "^1.19.0",
    "dotenv": "^8.2.0",
    "express": "^4.17.1",
    "knex": "^0.20.13",
    "moment": "^2.24.0",
    "pg": "^8.0.0",
    "tsc": "^1.20150623.0",
    "typescript": "^3.8.3"
  },
  "devDependencies": {
    "@babel/cli": "^7.8.4",
    "@babel/core": "^7.9.0",
    "@babel/preset-env": "^7.9.0",
    "@babel/preset-typescript": "^7.9.0",
    "@types/express": "^4.17.4"
  }

Babel is currently unused. I've tried changing module as well with no luck.

2 Answers 2

3

The issue is caused by "noEmit": true as that prevents any output, so of course I wasn't seeing any changes after running 'tsc'.

I was seeing errors messages specifically when running tsc server.ts because running with arguments or flags will ignore the tsconfig.json and use default flags, so running tsc server.ts --esModuleInterop produced expected results.

This is why I'm running it all from scratch, so I can make stupid mistakes like this, I guess. Hopefully anyone else who comes across this will have a better understanding of how 'tsc' and 'tsconfig.json' interact with each other.

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

Comments

0

I know you already solved this awhile back but wanted to comment - I ran into a similar issue when learning typescript and found this typescript outDir setting in tsconfig.json not working

It looks like maybe just compiling with tsc might be the way to go - it sounds like when we pass in file names like tsc server.ts it ignores the tsconfig.json file.

Typescript Documentation - https://www.typescriptlang.org/docs/handbook/tsconfig-json.html

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.