Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeScript import issues with apollo-server-express #927

Closed
dsebastien opened this issue Mar 30, 2018 · 29 comments
Closed

TypeScript import issues with apollo-server-express #927

dsebastien opened this issue Mar 30, 2018 · 29 comments

Comments

@dsebastien
Copy link

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 apollographql/apollo-cache-control#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
Copy link

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

@glasser
Copy link
Member

glasser commented Apr 2, 2018

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.

abernix added a commit that referenced this issue Apr 10, 2018
…ngth`.

This change seems necessary in order to meet the new type definitions for
`res.setHeader` which mandate that the argument be a string, or an array of
strings.  Those type definitions were introduced via the `@types/node@9`
series of typing updates provided in [0], [1], [2].

With any luck, this will fix the failures being exhibited in CircleCI
tests[3] after we landed those typing updates.

[0]: #907
[1]: #927
[2]: #939
[3]: https://circleci.com/gh/apollographql/apollo-server/1587
abernix added a commit that referenced this issue Apr 11, 2018
…ngth`. (#956)

This change seems necessary in order to meet the new type definitions for
`res.setHeader` which mandate that the argument be a string, or an array of
strings.  Those type definitions were introduced via the `@types/node@9`
series of typing updates provided in [0], [1], [2].

With any luck, this will fix the failures being exhibited in CircleCI
tests[3] after we landed those typing updates.

[0]: #907
[1]: #927
[2]: #939
[3]: https://circleci.com/gh/apollographql/apollo-server/1587
@whatupdave
Copy link

I fixed it by installing the types for graphql

npm i --save-dev @types/graphql

@abernix
Copy link
Member

abernix commented Sep 20, 2018

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
Copy link
Member

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

@AleksandarFaraj
Copy link

@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
Copy link
Contributor

kachkaev commented Oct 4, 2018

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

@cmcaboy
Copy link

cmcaboy commented Nov 17, 2018

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

@Vheissu
Copy link

Vheissu commented Nov 29, 2018

Installing the types as mentioned above fixes the issue.

@brianschardt
Copy link

brianschardt commented Dec 5, 2018

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
Copy link
Contributor

kachkaev commented Dec 9, 2018

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
Copy link

Same problem as @kachkaev

@fbjork
Copy link

fbjork commented Dec 19, 2018

+1

@liu-dongyu
Copy link

Same problem as @kachkaev.Any one can help ?

@sr523
Copy link

sr523 commented Dec 25, 2018

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

@nyze2oo9
Copy link

nyze2oo9 commented Jan 4, 2019

+1

1 similar comment
@manuga93
Copy link

manuga93 commented Jan 8, 2019

+1

@ghost
Copy link

ghost commented Feb 23, 2019

I fixed it by installing the types for graphql

npm i --save-dev @types/graphql

Thank you 🙏

@elliotec
Copy link

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

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

@leonetosoft
Copy link

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
Copy link

rob137 commented Mar 9, 2020

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 🤷‍♂️

@leonetosoft
Copy link

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

@johnnyoshika
Copy link

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

johnnyoshika added a commit to webhookbuddy/webhookbuddy-server that referenced this issue Apr 29, 2020
@petrogad
Copy link

petrogad commented Jun 3, 2020

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
Copy link
Member

abernix commented Jul 16, 2020

@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
Copy link

JVMartin commented Sep 21, 2020

@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
Copy link

AndresHMosqueda commented Oct 22, 2020

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
Copy link

victororlyk commented Dec 22, 2020

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
Copy link

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

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 20, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests