最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Apollo Client "Named export 'remove' not found" - Stack Overflow

programmeradmin2浏览0评论

I'm attempting to create an apollo client plugin for a Nuxt 3 application. It's currently throwing an error regarding a package called ts-invariant:

file:///Users/[my name]/Repositories/[project]/node_modules/@apollo/client/utilities/globals/fix-graphql.js:1
import { remove } from "ts-invariant/process/index.js";
         ^^^^^^
SyntaxError: Named export 'remove' not found. The requested module 'ts-invariant/process/index.js' 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 'ts-invariant/process/index.js';
const { remove } = pkg;

    at ModuleJob._instantiate (node:internal/modules/esm/module_job:124:21)
    at async ModuleJob.run (node:internal/modules/esm/module_job:181:5)
    at async Promise.all (index 0)
    at async ESMLoader.import (node:internal/modules/esm/loader:281:24)
    at async __instantiateModule__ (file:///Users/[my name]/Repositories/[project]/.nuxt/dist/server/server.mjs:4550:3)
[vite dev] Error loading external "/Users/[my name]/Repositories/[project]/node_modules/@apollo/client/core/index.js".
  at file://./.nuxt/dist/server/server.mjs:3170:289  
  at async __instantiateModule__ (file://./.nuxt/dist/server/server.mjs:4550:3)

I feel like I know enough about this error to know it has something to do with how Nuxt 3 deals with ESM, but I can't be for certain.

Here's the nuxt plugin:
plugins/apollo-client.js

import { defineNuxtPlugin } from "#app"
import { ApolloClient, InMemoryCache } from "@apollo/client/core"
import { DefaultApolloClient } from "@vue/apollo-posable"

export default defineNuxtPlugin((nuxtApp) => {
  const config = useRuntimeConfig()
  const apolloClient = new ApolloClient({
    uri: config.PUBLIC_API_ENDPOINT,
    cache: new InMemoryCache(),
  })
  nuxtApp.vueApp.provide(DefaultApolloClient, apolloClient)
})

In a normal scenario, I might use the nuxt-apollo munity module, but it is currently afk regarding a nuxt 3 port, so a plugin it is.

Here's some documentation I relied on for my plugin:
.html#vue-3

I'm attempting to create an apollo client plugin for a Nuxt 3 application. It's currently throwing an error regarding a package called ts-invariant:

file:///Users/[my name]/Repositories/[project]/node_modules/@apollo/client/utilities/globals/fix-graphql.js:1
import { remove } from "ts-invariant/process/index.js";
         ^^^^^^
SyntaxError: Named export 'remove' not found. The requested module 'ts-invariant/process/index.js' 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 'ts-invariant/process/index.js';
const { remove } = pkg;

    at ModuleJob._instantiate (node:internal/modules/esm/module_job:124:21)
    at async ModuleJob.run (node:internal/modules/esm/module_job:181:5)
    at async Promise.all (index 0)
    at async ESMLoader.import (node:internal/modules/esm/loader:281:24)
    at async __instantiateModule__ (file:///Users/[my name]/Repositories/[project]/.nuxt/dist/server/server.mjs:4550:3)
[vite dev] Error loading external "/Users/[my name]/Repositories/[project]/node_modules/@apollo/client/core/index.js".
  at file://./.nuxt/dist/server/server.mjs:3170:289  
  at async __instantiateModule__ (file://./.nuxt/dist/server/server.mjs:4550:3)

I feel like I know enough about this error to know it has something to do with how Nuxt 3 deals with ESM, but I can't be for certain.

Here's the nuxt plugin:
plugins/apollo-client.js

import { defineNuxtPlugin } from "#app"
import { ApolloClient, InMemoryCache } from "@apollo/client/core"
import { DefaultApolloClient } from "@vue/apollo-posable"

export default defineNuxtPlugin((nuxtApp) => {
  const config = useRuntimeConfig()
  const apolloClient = new ApolloClient({
    uri: config.PUBLIC_API_ENDPOINT,
    cache: new InMemoryCache(),
  })
  nuxtApp.vueApp.provide(DefaultApolloClient, apolloClient)
})

In a normal scenario, I might use the nuxt-apollo munity module, but it is currently afk regarding a nuxt 3 port, so a plugin it is.

Here's some documentation I relied on for my plugin:
https://v4.apollo.vuejs/guide-posable/setup.html#vue-3
https://v3.nuxtjs/docs/directory-structure/plugins

Share asked Jan 7, 2022 at 1:34 joshwcorbettjoshwcorbett 4182 gold badges7 silver badges18 bronze badges 1
  • I've made some progress by manually altering the package.json: github./apollographql/apollo-feature-requests/issues/… The lack of exports (in the latest version 3.5.10 I've tested) in @apollo/client and ts-invariant might cause this for Weback 5 based builds – Eric Burel Commented Mar 12, 2022 at 9:23
Add a ment  | 

3 Answers 3

Reset to default 4

Solved by including @apollo/client and ts-invariant/process into the nuxt build transpile like so:

  // nuxt.config.js
  // ...
  build: {
    postcss: {
      postcssOptions: require('./postcss.config.js')
    },
    transpile: [
      '@apollo/client',
      'ts-invariant/process',
    ],
  },
  // ...

I think I've pinpointed the underlying issue. Apollo Client (3.5.10 at the time of writing early 2022) is using "module":"index.js" to declare the path of the ESM exports. However it seems that Webpack 5 based bundlers do not support this. Using exports in the package.json fixes it for good for me.

You should upvote this feature request.

And here is my palliative until then, using a small script to alter the package.json.

None of the answers here worked for me. I had to add this include as well.

{
    test: /\.m?js$/,
    resolve: {
      fullySpecified: false,
    },
    include: [
      'graphql',
    ].map((name) => new RegExp(`node_modules/${name}`)),
  },
发布评论

评论列表(0)

  1. 暂无评论