nestjs / graphql

GraphQL (TypeScript) module for Nest framework (node.js) 🍷
https://docs.nestjs.com/graphql/quick-start
MIT License
1.46k stars 396 forks source link

ts error on graphql setup #920

Closed saravvij closed 4 years ago

saravvij commented 4 years ago

I'm submitting a...


[ ] Regression 
[x] Bug report
[ ] Feature request
[x] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior

I was trying to setup nestjs with graphql, I followed the document from here - https://docs.nestjs.com/graphql/quick-start

$ npm i --save @nestjs/graphql graphql-tools graphql $ npm i --save apollo-server-express

The server restarts failed with the below ts error,

node_modules/apollo-server-core/dist/ApolloServer.d.ts:5:8 - error TS1259: Module '"./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:270:1
    270 export = WebSocket;
        ~~~~~~~~~~~~~~~~~~~
    This module is declared with using 'export =', and can only be used with a default import when using the 'esModuleInterop' flag.

[2:49:43 PM] Found 1 error. Watching for file changes.

I had to fix this error by adding the config "esModuleInterop": true in tsconfig.json file. It would be great if this can be added by default in nest cli scaffolding or mentioned in the nestjs/graphql document somewhere.

Expected behavior

The ts error should not appear

Minimal reproduction of the problem with instructions

  1. Create a nest project using nest-cli
  2. Add graphql dependencies for express
  3. Restart the server to see ts error

What is the motivation / use case for changing the behavior?

Environment


Nest version:  7.2.0 (cli)


For Tooling issues:
- Node version: v14.3.0
- Platform: Mac

Others:

Deps:
"@nestjs/common": "^7.0.0",
"@nestjs/core": "^7.0.0",
"@nestjs/graphql": "^7.4.1",
"@nestjs/platform-express": "^7.0.0",
"apollo-server-express": "^2.14.1",
"graphql": "^15.0.0",
"graphql-tools": "^6.0.5",

ts:
"ts-node": "^8.6.2",
"typescript": "^3.7.4"            
fetche12345 commented 4 years ago

Add "esModuleInterop": true to your tsconfig.json and it should be good.

That works for me.

kamilmysliwiec commented 4 years ago

Fixed in the latest version of @nestjs/cli (specifically in its dependency @nestjs/schematics) and in the typescript-starter