graphql-typed-document-node icon indicating copy to clipboard operation
graphql-typed-document-node copied to clipboard

Error "The package may have incorrect main/module/exports specified in its package.json."

Open joulev opened this issue 2 years ago • 20 comments

I tried to upgrade my app to use v3.1.2, but without any other code changes, the following error shows up on build:

Error: Failed to resolve entry for package @graphql-typed-document-node/core. The package may have incorrect main/module/exports specified in its package.json.

Call stack
    at packageEntryFailure (file:///Users/joulev/dev/www/webapps/node_modules/.pnpm/[email protected]_@[email protected]/node_modules/vite/dist/node/chunks/dep-ca21228b.js:22005:11)
    at resolvePackageEntry (file:///Users/joulev/dev/www/webapps/node_modules/.pnpm/[email protected]_@[email protected]/node_modules/vite/dist/node/chunks/dep-ca21228b.js:22002:5)
    at tryNodeResolve (file:///Users/joulev/dev/www/webapps/node_modules/.pnpm/[email protected]_@[email protected]/node_modules/vite/dist/node/chunks/dep-ca21228b.js:21737:20)
    at Object.resolveId (file:///Users/joulev/dev/www/webapps/node_modules/.pnpm/[email protected]_@[email protected]/node_modules/vite/dist/node/chunks/dep-ca21228b.js:21488:28)
    at Object.handler (file:///Users/joulev/dev/www/webapps/node_modules/.pnpm/[email protected]_@[email protected]/node_modules/vite/dist/node/chunks/dep-ca21228b.js:44872:19)
    at file:///Users/joulev/dev/www/webapps/node_modules/.pnpm/[email protected]/node_modules/rollup/dist/es/shared/node-entry.js:24163:40
    at async PluginDriver.hookFirstAndGetPlugin (file:///Users/joulev/dev/www/webapps/node_modules/.pnpm/[email protected]/node_modules/rollup/dist/es/shared/node-entry.js:24063:28)
    at async resolveId (file:///Users/joulev/dev/www/webapps/node_modules/.pnpm/[email protected]/node_modules/rollup/dist/es/shared/node-entry.js:23007:26)
    at async ModuleLoader.resolveId (file:///Users/joulev/dev/www/webapps/node_modules/.pnpm/[email protected]/node_modules/rollup/dist/es/shared/node-entry.js:23271:15)
    at async Object.resolveId (file:///Users/joulev/dev/www/webapps/node_modules/.pnpm/[email protected]_@[email protected]/node_modules/vite/dist/node/chunks/dep-ca21228b.js:7972:10)

joulev avatar Feb 26 '23 17:02 joulev

This package is type-only package, and does not include any code. @n1ru4l any idea if we should adjust the config in bob?

dotansimha avatar Feb 27 '23 00:02 dotansimha

@dotansimha Should be supported by bob. 🤔 https://github.com/kamilkisiela/bob/pull/144/files#diff-691ec73579d989528bc5e4a7db013c029b3c478b3360d962ad5548a0ce42d1c1

I'll have a look.

n1ru4l avatar Feb 27 '23 07:02 n1ru4l

Hey @joulev, thanks for reporting this!

However, I have not been able to replicate this - I'll keep trying but in the meantime; have you tried a clean install (remove node_modules recursively and run install again)? Maybe share more info like the package.json, or ideally a repro?

enisdenjo avatar Feb 27 '23 09:02 enisdenjo

@enisdenjo Thanks for the reply. I retried just now with all node_modules removed and reinstalled, but the same thing happened.

My repo is a SvelteKit Vite app, where this package is used as part of GraphQL codegen; it's available here if you want a repro repo right now, but since it's a monorepo I will create a repro repo as minimal as possible instead, and will get back to you in a few hours.

joulev avatar Feb 27 '23 09:02 joulev

Thank you very much! That will help out definitely. In the meantime, I will try out a few things. 👍

enisdenjo avatar Feb 27 '23 09:02 enisdenjo

@enisdenjo Reproduction repo

Sorry as it might not be as minimal as you expected it to be, but I am not confident enough to make it any more minimal.

For the repo, if you run pnpm install and pnpm build it will fail. Changing the version of @graphql-typed-document-node/core to v3.1.1 fixes the build error.

joulev avatar Feb 27 '23 11:02 joulev

Vite is having issues importing @graphql-typed-document-node/core as a value, it worked with v3.1.1 because there we had an empty file that we used for value imports - but this changes. Am looking into ways of handling this.

A proper approach would also be to use type imports for type-only modules, I made a PR here: https://github.com/joulev/debug/pull/1.

enisdenjo avatar Feb 27 '23 12:02 enisdenjo

Thank you, it worked. (Don't worry about the deployment failure; it was because I didn't configure Vercel to build SvelteKit in that repo.)

Since I intend to keep the debug repo as-is so future people can come to this issue and see the reproduction repo as when it was reported, I think I will close your PR and commit to the main repo instead (see above).

joulev avatar Feb 27 '23 13:02 joulev

The plugin "vite:dep-scan" was triggered by this import

script:/home/viktord/Projects/renthome/dashboard/src/modules/notifications/modules/NotificationsBtnDeleteAll.vue?id=0:26:7:
  26 │ import '@graphql-typed-document-node/core'
     ╵        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

I started to get this error as well. I have no idea what happened but it worked many month and stopped

websitevirtuoso avatar Jun 02 '23 05:06 websitevirtuoso

you can try adding an exclusion rule for the @graphql-typed-document-node/core package in your Vite configuration. vite.config.js

It will help till this package will be fixed or vite will be fixed

export default {
  // ...other config options

  optimizeDeps: {
    exclude: ['@graphql-typed-document-node/core'],
  },
};

websitevirtuoso avatar Jun 03 '23 03:06 websitevirtuoso

For pnpm:

pnpm i graphql @graphql-typed-document-node/core
pnpm i -D typescript ts-node @graphql-codegen/cli @graphql-codegen/client-preset

https://the-guild.dev/graphql/codegen/docs/guides/react-vue#installation

emj365 avatar Jun 12 '23 09:06 emj365

I have the same also using Sveltekit + Vite. Both https://github.com/dotansimha/graphql-typed-document-node/issues/152#issuecomment-1586937530 and https://github.com/dotansimha/graphql-typed-document-node/issues/152#issuecomment-1574624687 do not work. It does seem that https://github.com/dotansimha/graphql-typed-document-node/issues/152#issuecomment-1446270040 works though.

rbozan avatar Aug 17 '23 13:08 rbozan

Using @graphql-codegen/[email protected], in the codegen.ts file I added the config for the client preset like this:

import type { CodegenConfig } from '@graphql-codegen/cli'

const config: CodegenConfig = {
  overwrite: true,
  schema: 'src/lib/schema.graphql',
  documents: 'src/**/*.ts',
  generates: {
    'src/lib/gql/': {
      preset: 'client',
      plugins: [],
      config: {
        useTypeImports: true // The updated setting.
      }
    }
  }
}

export default config

It fixed the problem with Vite.

romelperez avatar Aug 21 '23 23:08 romelperez

[email protected] + [email protected] configured with tsconfig.json

{
  "compilerOptions": {
    "module": "ESNext",
    "verbatimModuleSyntax": true,
  }
}

Refuses to build with this error

Failed to resolve entry for package "@graphql-typed-document-node/core". The package may have incorrect main/module/exports specified in its package.json.

Enabling useTypeImports unfortunately doesn't work for me. I also unsuccessfully tried adding the following to vite.config.ts:

export default defineConfig({
  // ...
  optimizeDeps: {
    exclude: ['@graphql-typed-document-node/core'],
  },
}

Would it be possible to perhaps re-add the empty JS main file back into the repository? verbatimModuleSyntax is necessary to make typescript annotate type-only imports with type.

mogelbrod avatar Feb 05 '24 14:02 mogelbrod

Same problem here with Vite v5.1.5 / Vue3

export default defineConfig({
  // ...
  optimizeDeps: {
    exclude: ['@graphql-typed-document-node/core'],
  },
}

Doesn't work for me either

{
  "compilerOptions": {
    "module": "ESNext",
    "verbatimModuleSyntax": true,
  }
}

Also that fix doesn't work for my setup

snmabaur avatar Mar 06 '24 21:03 snmabaur

I'm hitting the same problem when building an app with vite:

Error: [commonjs--resolver] Failed to resolve entry for package "@graphql-typed-document-node/core". The package may have incorrect main/module/exports specified in its package.json.

I already had useTypeImports: true in my codegen config, so that doesn't fix it.

Removing "verbatimModuleSyntax": true from my tsconfig file does fix it (regardless of whether useTypeImports is true or false).

Adding the optimizeDeps value to my vite config didn't do any good.

I also tried installing @graphql-typed-document-node/core v3.1.1, but it seems like for this to be compatible with GraphQL Code Generator I'd also have to revert to a significantly older version of their client preset (and possibly other packages).


Summing up: removing "verbatimModuleSyntax": true from my tsconfig is the only thing that has fixed the build error so far.

@enisdenjo Echoing @mogelbrod: would it be possible to readd the empty JS file until some other solution is found?

mmirus avatar Mar 20 '24 17:03 mmirus

I have the same problem here with Vite 5.2.8 and Vue3

Error: [commonjs--resolver] Failed to resolve entry for package "@graphql-typed-document-node/core". The package may have incorrect main/module/exports specified in its package.json.

snmabaur avatar Apr 05 '24 08:04 snmabaur