apollographql / apollo-server

šŸŒ Ā Spec-compliant and production ready JavaScript GraphQL server that lets you develop in a schema-first way. Built for Express, Connect, Hapi, Koa, and more.
https://www.apollographql.com/docs/apollo-server/
MIT License
13.75k stars 2.03k forks source link

TypeScript import issues with apollo-server-express #927

Closed dsebastien closed 5 years ago

dsebastien commented 6 years ago

Hello,

Yesterday while I was trying out Apollo for the first time with express and TypeScript, I've stumbled upon the following issue when compiling using TSC:

node_modules/apollo-cache-control/lib/index.d.ts(20,16): error TS2665: Invalid module name in augmentation. Module 'graphql/type/definition' resolves to an untyped module at 'C:/dev/wks/bedeals-mvp/functions/node_modules/graphql/type/definition.js', which cannot be augmented..

At first I filed https://github.com/apollographql/apollo-cache-control/issues/4 but then realized that my imports were causing the issue.

This import causes the issue with TypeScript: import {graphiqlExpress, graphqlExpress} from "apollo-server-express"; while it works fine with this one: const {graphqlExpress, graphiqlExpress} = require('apollo-server-express');.

You may say "that's the way to import it", but the first import was added for me by IntelliJ's auto-import, which is why I found this to be disturbing. I wasted a lot of time thinking that my node_modules folder wasn't okay, that my TSC version or config was incorrect, ...

It would be nice to have better TypeScript support

stanleyeosakul commented 6 years ago

Nice fix! I agree, I wish there was better Typescript support.

glasser commented 6 years ago

I'm not sure that I follow ā€” shouldn't we support import if your TS/Node is otherwise set up to support it?

I'll note that I saw a version of this error myself while working on creating apollo-cache-control@0.1.0 but it resolved itself after... running npm install inside apollo-server-core. I thought maybe it was just an artifact of our lerna setup but if it's affecting end users, that's an issue.

whatupdave commented 6 years ago

I fixed it by installing the types for graphql

npm i --save-dev @types/graphql
abernix commented 5 years ago

Can anyone confirm that this is still a problem for them with Apollo Server 2?

While this issue has a number of up-votes, it's gone otherwise silent and it's hard to determine if this problem is still affecting users so we can prioritize accordingly.

Feedback would be appreciated!

hwillson commented 5 years ago

Sounds like this has been resolved. Let us know if this is still an issue. Thanks!

AleksandarFaraj commented 5 years ago

@abernix I just got this issue and fixed it using whatupdave's solution. It seems it could be easily fixable by just including @types/graphql as a dependency for the cache-control package?

kachkaev commented 5 years ago

Same thing here as @AleksandarFaraj describes. Using apollo-server 2.1.0.

cmcaboy commented 5 years ago

This fixed my issue! Thank you so much @dsebastien!

Vheissu commented 5 years ago

Installing the types as mentioned above fixes the issue.

brianschardt commented 5 years ago

This is still a problem for me "apollo-server-express": "^2.1.0" "typescript": "^3.1.2" "@types/graphql": "^14.0.3",

NEED HELP

kachkaev commented 5 years ago

Here's the output of my linting tsc, even after installing @types/graphql:

node_modules/apollo-server-core/dist/types.d.ts:4:8 - error TS1192: Module '"/path/to/graphql-servera/node_modules/@types/ws/index"' has no default export.

4 import WebSocket from 'ws';
         ~~~~~~~~~

node_modules/apollo-server-express/dist/ApolloServer.d.ts:1:8 - error TS1192: Module '"/path/to/graphql-server/node_modules/@types/express/index"' has no default export.

1 import express from 'express';
         ~~~~~~~

node_modules/apollo-server-express/dist/ApolloServer.d.ts:2:8 - error TS1192: Module '"/path/to/graphql-server/node_modules/@types/cors/index"' has no default export.

2 import corsMiddleware from 'cors';

Adding "skipLibCheck": true, has helped as a workaround, but the issue is still topical.

FrankSandqvist commented 5 years ago

Same problem as @kachkaev

fbjork commented 5 years ago

+1

liu-dongyu commented 5 years ago

Same problem as @kachkaev.Any one can help ?

sr523 commented 5 years ago

+1 same problem. excuse my ignorance, but what does skipLibCheck do and why does it work?

nyze2oo9 commented 5 years ago

+1

manuga93 commented 5 years ago

+1

ghost commented 5 years ago

I fixed it by installing the types for graphql

npm i --save-dev @types/graphql

Thank you :pray:

elliotec commented 5 years ago

Adding @types/graphql didn't work for me, but as found here: https://github.com/apollographql/apollo-server/issues/669

The solution was to setĀ "moduleResolution": "node"Ā atĀ tsconfig.json

leonetosoft commented 4 years ago

Hello!

The fix for the problem is to install @type/graphql.

However, I realized that the latest versions of @ types / graphql completely removed the type definitions, which became part of the graphql package. If you download the most current version you will see that it is empty and contains no definition. Then install the version:

npm install@types/graphql@14.2.3

Hope this helps.

rob137 commented 4 years ago

Adding @types/graphql didn't work for me, but as found here: #669

The solution was to setĀ "moduleResolution": "node"Ā atĀ tsconfig.json

Hello!

The fix for the problem is to install @type/graphql.

However, I realized that the latest versions of @ types / graphql completely removed the type definitions, which became part of the graphql package. If you download the most current version you will see that it is empty and contains no definition. Then install the version:

npm install@types/graphql@14.2.3

Hope this helps.

Neither of these worked for me :man_shrugging:

leonetosoft commented 4 years ago

@rob137 Run command: npm install @types/graphql@14.2.3 ? Please show me error in screen.

johnnyoshika commented 4 years ago

Similar to @kachkaev, I got this error when running in debug mode in VS Code:

node_modules/apollo-server-express/dist/ApolloServer.d.ts:1:8 - error TS1259: Module '"/path/to/apollo-server-express/node_modules/@types/express/index"' can only be default-imported using the 'esModuleInterop' flag

1 import express from 'express';
         ~~~~~~~

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

node_modules/apollo-server-express/dist/ApolloServer.d.ts:2:8 - error TS1259: Module '"/path/to/node_modules/@types/cors/index"' can only be default-imported using the 'esModuleInterop' flag

2 import corsMiddleware from 'cors';
         ~~~~~~~~~~~~~~

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

Interstingly it worked fine when I ran the application from terminal, so it was only in VS Code debug mode that was causing problems.

Like @kachkaev, my workaround was to add "skipLibCheck": true to tsconfig.json

petrogad commented 4 years ago

Received this error too and ended up with the following:

node_modules/apollo-server-express/node_modules/@types/express/index.d.ts:98:50 - error TS2694: Namespace '"/node_modules/@types/express-serve-static-core/index"' has no exported member 'Params'.

98     interface ErrorRequestHandler<P extends core.Params = core.ParamsDictionary> extends core.ErrorRequestHandler<P> { }
                                                    ~~~~~~

node_modules/apollo-server-express/node_modules/@types/express/index.d.ts:98:64 - error TS2694: Namespace '"/node_modules/@types/express-serve-static-core/index"' has no exported member 'ParamsDictionary'.

98     interface ErrorRequestHandler<P extends core.Params = core.ParamsDictionary> extends core.ErrorRequestHandler<P> { }
                                                                  ~~~~~~~~~~~~~~~~

node_modules/apollo-server-express/node_modules/@types/express/index.d.ts:98:90 - error TS2315: Type 'ErrorRequestHandler' is not generic.

98     interface ErrorRequestHandler<P extends core.Params = core.ParamsDictionary> extends core.ErrorRequestHandler<P> { }
                                                                                            ~~~~~~~~~~~~~~~~~~~~~~~~~~~

node_modules/apollo-server-express/node_modules/@types/express/index.d.ts:107:38 - error TS2694: Namespace '"/node_modules/@types/express-serve-static-core/index"' has no exported member 'Params'.

107     interface Request<P extends core.Params = core.ParamsDictionary> extends core.Request<P> { }
                                         ~~~~~~

node_modules/apollo-server-express/node_modules/@types/express/index.d.ts:107:52 - error TS2694: Namespace '"/node_modules/@types/express-serve-static-core/index"' has no exported member 'ParamsDictionary'.

107     interface Request<P extends core.Params = core.ParamsDictionary> extends core.Request<P> { }
                                                       ~~~~~~~~~~~~~~~~

node_modules/apollo-server-express/node_modules/@types/express/index.d.ts:107:78 - error TS2315: Type 'Request' is not generic.

107     interface Request<P extends core.Params = core.ParamsDictionary> extends core.Request<P> { }
                                                                                 ~~~~~~~~~~~~~~~

node_modules/apollo-server-express/node_modules/@types/express/index.d.ts:108:45 - error TS2694: Namespace '"/node_modules/@types/express-serve-static-core/index"' has no exported member 'Params'.

108     interface RequestHandler<P extends core.Params = core.ParamsDictionary> extends core.RequestHandler<P> { }
                                                ~~~~~~

node_modules/apollo-server-express/node_modules/@types/express/index.d.ts:108:59 - error TS2694: Namespace '"/node_modules/@types/express-serve-static-core/index"' has no exported member 'ParamsDictionary'.

108     interface RequestHandler<P extends core.Params = core.ParamsDictionary> extends core.RequestHandler<P> { }
                                                              ~~~~~~~~~~~~~~~~

node_modules/apollo-server-express/node_modules/@types/express/index.d.ts:108:85 - error TS2315: Type 'RequestHandler' is not generic.

108     interface RequestHandler<P extends core.Params = core.ParamsDictionary> extends core.RequestHandler<P> { }
                                                                                        ~~~~~~~~~~~~~~~~~~~~~~

node_modules/apollo-server-express/node_modules/@types/express/index.d.ts:110:54 - error TS2315: Type 'Response' is not generic.

110     export interface Response<ResBody = any> extends core.Response<ResBody> { }

After a while of debugging, trying various types.. I ended up just adding skipLibCheck: true to my tsconfig.json file.. which seems quite wrong.

My full tsconfig.json file:

{
    "compilerOptions": {
        "skipLibCheck": true,
        "module": "commonjs",
        "declaration": true,
        "removeComments": true,
        "emitDecoratorMetadata": true,
        "experimentalDecorators": true,
        "target": "es6",
        "sourceMap": true,
        "outDir": "./dist",
        "allowSyntheticDefaultImports": true,
        "esModuleInterop": true
    },
    "exclude": ["node_modules", "src/migrations"]
}
abernix commented 4 years ago

@farukbigez graphqlExpress is from Apollo Server 1.x. It hasn't been exported from that module ever since 2.0 was released. See a recent getting started section in our documentation for 2.x. If you're migrating from 1.x, see the migration guide.

JVMartin commented 3 years ago

@hwillson This is still a problem on apollo server 2.16.1.

> nichehacks-api@0.0.0 build /home/jacob/code/nichehacks/nichehacks-api
> nest build

node_modules/apollo-server-core/dist/ApolloServer.d.ts:5:8 - error TS1259: Module '"/home/jacob/code/nichehacks/nichehacks-api/node_modules/@types/ws/index"' can only be default-imported using the 'esModuleInterop' flag

5 import WebSocket from 'ws';
         ~~~~~~~~~

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

Found 1 error(s).

npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! nichehacks-api@0.0.0 build: `nest build`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the nichehacks-api@0.0.0 build script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/jacob/.npm/_logs/2020-09-21T19_21_36_909Z-debug.log

Can't build without skipLibCheck set to true, which is dangerous and ill advised.

AndresHMosqueda commented 3 years ago

During the build process of the app I get the following error:

node_modules/apollo-server-express/node_modules/apollo-server-core/dist/plugin/index.d.ts:1:13 error TS1005: '=' expected.

Screen Shot 2020-10-22 at 11 36 19

I have not found the fix for this yet, I've deleted node_modules folder and package-lock.json but still not working.

It would be nice to have some help....

tsconfig.json

{
  "compilerOptions": {
    "target": "es5",
    "moduleResolution": "node",
    "strict": true,
    "esModuleInterop": true,
    "skipLibCheck": true,
    "outDir": "./dist",
    "declaration": true
  },
  "include": [
    "src/**/*"
  ],
  "exclude": [
    "node_modules",
    "**/*.spec.ts",
    "**/*.test.ts"

  ]

}

package.json

"dependencies": {
    "@apollo/federation": "^0.10.2",
    "@types/bluebird": "^3.5.27",
    "@types/cookie-parser": "^1.4.2",
    "@types/jest": "^24.0.19",
    "@types/mysql": "^2.15.7",
    "@types/nanoid": "^2.0.0",
    "@types/node": "^14.10.1",
    "@types/umzug": "^2.2.2",
    "@types/validator": "^10.11.3",
    "apollo-cache-control": "^0.10.0",
    "apollo-cache-inmemory": "^1.6.6",
    "apollo-client": "^2.6.4",
    "apollo-datasource-rest": "^0.6.1",
    "apollo-link": "^1.2.12",
    "apollo-link-context": "^1.0.20",
    "apollo-link-http": "^1.5.17",
    "apollo-link-rest": "^0.7.3",
    "apollo-link-schema": "^1.2.3",
    "apollo-server": "^2.12.0",
    "apollo-server-express": "^2.12.0",
    "apollo-server-plugin-response-cache": "^0.3.1",
    "apollo-server-testing": "^2.9.0",
    "axios": "^0.19.2",
    "cookie-parser": "^1.4.4",
    "cron": "^1.7.2",
    "elastic-apm-node": "^3.3.0",
    "express": "^4.17.1",
    "express-graphql": "^0.9.0",
    "graphql": "^14.5.8",
    "graphql-import": "^0.7.1",
    "graphql-schema-linter": "^0.2.1",
    "jsonwebtoken": "^8.5.1",
    "jwk-to-pem": "^2.0.3",
    "kafka-node": "^4.1.3",
    "link": "^0.1.5",
    "log4js": "^5.0.0",
    "mysql2": "^1.7.0",
    "nanoid": "^3.1.7",
    "node": "^14.5.0",
    "node-cache": "^5.1.1",
    "node-fetch": "^2.6.0",
    "promise-retry": "^1.1.1",
    "rimraf": "^3.0.0",
    "save": "^2.4.0",
    "sequelize": "^5.19.5",
    "sequelize-cli": "^5.5.1",
    "soap": "^0.30.0",
    "ts-jest": "^24.1.0",
    "typescript": "^3.6.3",
    "umzug": "^2.2.0"
  },
  "devDependencies": {
    "@types/graphql": "^14.5.0",
    "graphql-tag": "^2.10.1",
    "graphql-tools": "^4.0.5",
    "jest": "^24.8.0",
    "jest-cli": "^24.8.0"
  },
  "jest": {
    "testEnvironment": "node",
    "testPathIgnorePatterns": [
      "/node_modules/",
      "/dist/"
    ],
typescript
npm
react-apollo
apollo-server
tsconfig
victororlyk commented 3 years ago

well, it didn't work out for me. installing @types/graphql neither did work "@types/graphql": "14.2.3". Compiler options didn't help "moduleResolution": "node".

Don't know why this is closed.

lorstenoplo commented 3 years ago

This what i do, just cast such things as any to workaround the problem const server = new (ApolloServer as any) ({ typeDefs, resolvers, });