I'm currently trying to compile a server with tsc. While the whole projects works fine when I run it with ts-node or ts-jest it won't compile with tsc.
I get the following errors:
$ tsc
node_modules/apollo-server-express/dist/ApolloServer.d.ts:1:8 - error TS1192: Module '"node_modules/@types/express/index"' has no default export.
1 import express from 'express';
~~~~~~~
node_modules/apollo-server/dist/index.d.ts:2:8 - error TS1192: Module '"http"' has no default export.
2 import http from 'http';
~~~~
Here is a shortened version of my package.json file:
"scripts": {
"build": "tsc",
...
},
"dependencies": {
"apollo-server": "^2.0.0-rc.0",
"apollo-server-express": "^2.0.0-rc.0",
...
},
"devDependencies": {
"@types/express": "^4.16.0",
"@types/jest": "^22.2.3",
"@types/node": "^10.3.3",
"jest": "^22.4.3",
"jest-cli": "^22.4.3",
"ts-jest": "^22.4.6",
"ts-node": "^6.0.3",
"typescript": "^2.9.2",
...
},
If I'm correct this is a bug and I could submit a pull request. I think that both of the imports needs to be replaced with a import * as express from "express" and import * as http from "http". The first import is correct in the master branch and the second file doesn't exist in the master branch. Am I missing something or is this really wrong?
Also Typescript doesn't complain about the import express from "express" in the Apollo-Server/index.ts file, although it should be also wrong.
I've found my problem, I needed to add "esModuleInterop": true, to my tsconfig.json file.
Adding "esModuleInterop": true opens up a lot of other issues in my code base.
I don't think libraries should force a TypeScript compiler option like esModuleInterop. I'd consider this a bug in apollo-server.
I've filed a pull request at https://github.com/apollographql/apollo-server/pull/2143 to fix the import statements (again?).
EDIT: Nevermind. Fix is to enable esModuleInterop and remove * as from any import statements.
After reviewing #1699, I did some more digging. According to https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-7.html,
Note: The new behavior is added under a flag to avoid unwarranted breaks to existing code bases. We highly recommend applying [esModuleInterop] both to new and existing projects. For existing projects, namespace imports (import * as express from "express"; express();) will need to be converted to default imports (import express from "express"; express();)
After I enabled esModuleInterop in my own project, it caused problems that appeared to be related to typeof were fixed by removing * as from import statements. E.g., changing from
import * as express from 'express'
to
import express from 'express'
Most helpful comment
I don't think libraries should force a TypeScript compiler option like
esModuleInterop. I'd consider this a bug in apollo-server.