nuxt / bridge

🌉 Experience Nuxt 3 features on existing Nuxt 2 projects
MIT License
270 stars 29 forks source link

"external_defu_default(...) is not a function" when defining axios create method in plugin #455

Open Tcharlyson opened 2 years ago

Tcharlyson commented 2 years ago

Environment


Reproduction

https://codesandbox.io/s/confident-wildflower-cjzhey

Describe the bug

I've set a plugin :

export default <Plugin>function ({ app, $axios }, inject: Inject) {
  const snackbarStore = useSnackbarStore()
  const authStore = useAuthStore()

  const api = $axios.create({
    headers: {
      common: {
        Accept: 'application/json',
        'Accept-Language': `${app.i18n.locale}, fr-FR`,
      },
    }
  })

  inject('api', api)
}

On dev mode everything's fine but when I build and start, I got this error :

[nuxt] [request error] external_defu_default(...) is not a function
  at Function.create (./server/chunks/app/server.mjs:22020:59)  
  at plugins_api (./server/chunks/app/server.mjs:22223:26)  
  at createApp (./server/chunks/app/server.mjs:22723:15)  
  at async __webpack_exports__.default (./server/chunks/app/server.mjs:22821:11)  
  at async renderToString (./server/chunks/handlers/renderer.mjs:268:19)  
  at async ./server/chunks/handlers/renderer.mjs:601:20  
  at async ./server/node_modules/h3/dist/index.mjs:417:19  
  at async Server.nodeHandler (./server/node_modules/h3/dist/index.mjs:367:7)

Line 22020 is :

create(options) {
  return createAxiosInstance(external_defu_default()(options, this.defaults));
}

Additional context

No response

Logs

No response

aldarund commented 2 years ago

In the provided example for me its just infinite loop into plugin with printing console.log from it. Its caused by defu error, but for some reason that error in plugin cause infinite loop ( possible another issue?). To get defu error need wrap axios.create from plugin into try catch e.g. like this

// eslint-disable-next-line import/named
import { NuxtAxiosInstance } from '@nuxtjs/axios'
import { Inject } from '@nuxt/types/app'
import type { Plugin } from '@nuxt/types'

export default <Plugin>function ({ $axios }, inject: Inject) {
  console.log('Plugin is called')
  // Create a custom axios instance
  try {
  const api = $axios.create({
    headers: {
      common: {
        Accept: 'application/json'
      }
    }
  }) as NuxtAxiosInstance
  } catch (e) {
      console.log(e)
  }

} 

which will produce following error:

Plugin is called
TypeError: external_defu_default(...) is not a function
    at Function.create (file:///d:/sss/.output/server/chunks/server.mjs:16425:60)
    at api (file:///d:/sss/.output/server/chunks/server.mjs:16542:30)
    at createApp (file:///d:/sss/.output/server/chunks/server.mjs:16734:16)
    at async __webpack_exports__.default (file:///d:/sss/.output/server/chunks/server.mjs:16829:12)
    at async Object.renderToString (file:///d:/sss/.output/server/chunks/renderer.mjs:274:19)
    at async file:///d:/sss/.output/server/chunks/renderer.mjs:658:20
    at async file:///d:/sss/.output/server/node_modules/h3/dist/index.mjs:438:19
    at async Server.nodeHandler (file:///d:/sss/.output/server/node_modules/h3/dist/index.mjs:384:7)
brolnickij commented 2 years ago

I have the same problem

npm i --save-dev defu@5.0.1 solves this problem

existe-deja commented 1 year ago

insatlling defu doesn't solve the problem on

------------------------------
- Operating System: `Linux`
- Node Version:     `v16.19.0`
- Nuxt Version:     `2.16.2`
- Nitro Version:    `1.0.0`
- Package Manager:  `yarn@1.22.19`
- Builder:          `webpack`
- User Config:      `bridge`, `target`, `nitro`, `components`, `runtimeConfig`, `app`, `css`, `loading`, `plugins`, `devServerHandlers`, `modules`, `build`, `router`, `twicpics`, `i18n`, `toast`, `sentry`, `content`, `serverHandlers`, `devServer`, `typescript`, `buildModules`
- Runtime Modules:  `@nuxtjs/sentry@6.0.3`, `@nuxtjs/axios@5.13.6`, `@nuxtjs/i18n@7.3.1`, `@nuxtjs/toast@3.3.1`, `@twicpics/components/nuxt2@0.15.1`
- Build Modules:    `()`, `@nuxt/bridge@3.0.0-27928371.c30fa0f`
------------------------------
brolnickij commented 1 year ago

confirmed, for the latest build @nuxt/bridge downgrade for defu - not working :((

brolnickij commented 1 year ago

im finded solution https://github.com/unjs/defu/issues/81#issuecomment-1458367904