graphql-tools icon indicating copy to clipboard operation
graphql-tools copied to clipboard

SyntaxError when importing graphql-tools as a module from a JS file

Open axibyte opened this issue 5 years ago • 12 comments

Describe the bug After upgrading graphql to version 16.0.0, importing graphql-tools raise SyntaxError: Named export 'Kind' not found. The requested module 'graphql' is a CommonJS module, which may not support all module.exports as named exports.

To Reproduce 1- Create a new project 2- Add graphql package 3- Add graphql-tools package 4- Import graphql-tools as ES module

Expected behavior graphql-tools should import the ES version of graphql when loaded as module, not the CommonJS module

Environment:

  • OS: Windows 10
  • @graphql-tools/...: 8.5.1
  • graphql: 16.0.0
  • NodeJS: 14.16.1

axibyte avatar Nov 01 '21 09:11 axibyte

Adding a bit more detail.

{full_path}/node_modules/@graphql-tools/utils/index.mjs:1
import { parse, isNonNullType, GraphQLError, Kind, valueFromAST, print, isObjectType, isListType, isSpecifiedDirective, astFromValue, isSpecifiedScalarType, isIntrospectionType, isInterfaceType, isUnionType, isInputObjectType, isEnumType, isScalarType, GraphQLDeprecatedDirective, specifiedRules, concatAST, validate, versionInfo, buildClientSchema, visit, TokenKind, Source, isTypeSystemDefinitionNode, getNamedType, GraphQLString, GraphQLNonNull, GraphQLList, GraphQLID, GraphQLBoolean, GraphQLFloat, GraphQLInt, GraphQLObjectType, GraphQLInterfaceType, GraphQLInputObjectType, GraphQLDirective, GraphQLUnionType, GraphQLEnumType, GraphQLScalarType, isNamedType, getNullableType, isLeafType, GraphQLSchema, isDirective, isCompositeType, doTypesOverlap, getOperationAST, getDirectiveValues, GraphQLSkipDirective, GraphQLIncludeDirective, typeFromAST, isAbstractType, getOperationRootType, TypeNameMetaFieldDef, buildASTSchema } from 'graphql';
                                             ^^^^
SyntaxError: Named export 'Kind' not found. The requested module 'graphql' is a CommonJS module, which may not support all module.exports as named exports.
CommonJS modules can always be imported via the default export, for example using:

import pkg from 'graphql';
const { parse, isNonNullType, GraphQLError, Kind, valueFromAST, print, isObjectType, isListType, isSpecifiedDirective, astFromValue, isSpecifiedScalarType, isIntrospectionType, isInterfaceType, isUnionType, isInputObjectType, isEnumType, isScalarType, GraphQLDeprecatedDirective, specifiedRules, concatAST, validate, versionInfo, buildClientSchema, visit, TokenKind, Source, isTypeSystemDefinitionNode, getNamedType, GraphQLString, GraphQLNonNull, GraphQLList, GraphQLID, GraphQLBoolean, GraphQLFloat, GraphQLInt, GraphQLObjectType, GraphQLInterfaceType, GraphQLInputObjectType, GraphQLDirective, GraphQLUnionType, GraphQLEnumType, GraphQLScalarType, isNamedType, getNullableType, isLeafType, GraphQLSchema, isDirective, isCompositeType, doTypesOverlap, getOperationAST, getDirectiveValues, GraphQLSkipDirective, GraphQLIncludeDirective, typeFromAST, isAbstractType, getOperationRootType, TypeNameMetaFieldDef, buildASTSchema } = pkg;

    at ModuleJob._instantiate (internal/modules/esm/module_job.js:104:21)
    at async ModuleJob.run (internal/modules/esm/module_job.js:149:5)
    at async Loader.import (internal/modules/esm/loader.js:166:24)
    at async Object.loadESM (internal/process/esm_loader.js:68:5)
    ```

the-noob avatar Nov 01 '21 10:11 the-noob

We have some ESM tests here. Could you help us to reproduce it? https://github.com/ardatan/graphql-tools/blob/master/scripts/test-esm.mjs

ardatan avatar Nov 01 '21 10:11 ardatan

Not sure I can do it in that setup might be a packaging issue. Jere's a very simple package setup that replicates the issue.

{
  "name": "test_gql",
  "type": "module",
  "main": "index.js",
  "dependencies": {
    "@graphql-tools/schema": "^8.3.1",
    "graphql": "^16.0.0"
  }
}
# index.js

import { makeExecutableSchema } from "@graphql-tools/schema";

const typeDefs = `
  type User {
    id: Int!
    firstName: String
    lastName: String
  }

  type Query {
    users: [User]
  }
`;

const users = [
  { id: 1, firstName: 'first', lastName: 'last' },
];

const resolvers = {
  Query: {
    users: () => users,
  },
};

export const schema = makeExecutableSchema({
  typeDefs,
  resolvers,
});

node index.js throws the above error

Node: v14.15.4 NPM: 6.14.10

the-noob avatar Nov 01 '21 14:11 the-noob

I think it is working fine here in the sandbox with the latest version of packages and Node v14; https://codesandbox.io/s/restless-morning-22w88?file=/src/index.mjs

Could you help me to reproduce your issue in CodeSandbox?

ardatan avatar Nov 02 '21 09:11 ardatan

Tried a bit but unfortunately it doesn't behave in the same way, not sure if each codesandbox is fully containerised or inherits some loading mechanism that hides this.

You should be able to reproduce if you create a local folder and save the package.json and index.js files with the above content. Running node index.js should throw the error.

the-noob avatar Nov 02 '21 09:11 the-noob

I already tried locally as well but still I couldn't reproduce the issue you have.

ardatan avatar Nov 02 '21 09:11 ardatan

Closing this issue for now. Please feel free to create a new one with a reproduction.

ardatan avatar Jan 13 '22 04:01 ardatan

I'm getting this on Windows but not on OSX, running the exact same code.

mattisx avatar Feb 11 '22 06:02 mattisx

@mattisx Can you provide a minimal reproduction repository?

n1ru4l avatar Feb 11 '22 08:02 n1ru4l

If this helps anyone else, my issue was that I was on Node v14.* and switching to Node 16.14.0 or later fixed the issue after a rm -rf node_modules && [yarn|npm] install

GentryRiggen avatar Mar 08 '22 19:03 GentryRiggen

If this helps anyone else, my issue was that I was on Node v14.* and switching to Node 16.14.0 or later fixed the issue after a rm -rf node_modules && [yarn|npm] install

I can confirm I got this working using 16.14.0. I was on 14.16.0 and was giving error: error - unhandledRejection: SyntaxError: Named export 'Kind' not found. The requested module 'graphql' is a CommonJS module, which may not support all module.exports as named exports. CommonJS modules can always be imported via the default export, for example using:

I used nvm to change my version, command below (you'll need nvm first to use): nvm install 16.14.0 nvm use 16.14.0

inspiretk avatar Apr 22 '22 04:04 inspiretk

If this helps anyone else, my issue was that I was on Node v14.* and switching to Node 16.14.0 or later fixed the issue after a rm -rf node_modules && [yarn|npm] install

I can confirm I got this working using 16.14.0. I was on 14.16.0 and was giving error: error - unhandledRejection: SyntaxError: Named export 'Kind' not found. The requested module 'graphql' is a CommonJS module, which may not support all module.exports as named exports. CommonJS modules can always be imported via the default export, for example using:

I used nvm to change my version, command below (you'll need nvm first to use): nvm install 16.14.0 nvm use 16.14.0

I can confirm, I've faced the same issue with node 14.16.1 and after upgrading to 16.14.0 everything worked as expected.

Thank you @GentryRiggen and @inspiretk and everyone else for the help. Much appreciated.

inc16sec avatar Nov 15 '22 13:11 inc16sec