Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Tcharlyson opened this issue Aug 3, 2022 · 5 comments

Comments

@Tcharlyson
Copy link

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

@Tcharlyson Tcharlyson changed the title "external_defu_default(...) is not a function" when overriding axios create method in plugin "external_defu_default(...) is not a function" when defining axios create method in plugin Aug 3, 2022
@aldarund
Copy link

aldarund commented Aug 3, 2022

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
Copy link

brolnickij commented Aug 9, 2022

I have the same problem

npm i --save-dev [email protected] solves this problem

@existe-deja
Copy link

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:  `[email protected]`
- 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/[email protected]`, `@nuxtjs/[email protected]`, `@nuxtjs/[email protected]`, `@nuxtjs/[email protected]`, `@twicpics/components/[email protected]`
- Build Modules:    `()`, `@nuxt/[email protected]`
------------------------------

@brolnickij
Copy link

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

@brolnickij
Copy link

im finded solution unjs/defu#81 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants