axios: adapter is not a function (EDIT: after 1st request)

Edit:

I originally thought this was only happening in the DELETE request. It turns out that any request after the first request fails . So if I send the POST and then the POST again, the 2nd time it fails.

TypeError: adapter is not a function
    at dispatchRequest (dispatchRequest.js:52)
    at async http:/localhost:3004/launchpad/static/js/205.chunk.js:537

Hi there

In the same page we have several calls to axios.get, axios.post and those work fine.

But using axios.delete I’m getting a weird error

adapter is not a function

I inspected into axios’ code and found that effectively, defaults.adapter is undefined

image (20)

Weird thing is that in other verbs (get, post), adapter is a function (I think is xhrAdapter or something like that)

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 6
  • Comments: 24 (1 by maintainers)

Commits related to this issue

Most upvoted comments

import axiosOriginal from 'axios'
import adapter from 'axios/lib/adapters/xhr'

const axios = axiosOriginal.create({ adapter })

XMLHttpRequest is not defined

I’ve had the same problem. But my problem was, that I tried to use Axios inside of a service worker. Unfortunately that doesn’t work since axios can’t work inside of a worker, because of: https://github.com/axios/axios/issues/1219 So the above solution didn’t work (obviously)

I have encountered the same issue when I use axios in chrome extension background.js.

@PedroFolego @tktcorporation @MakarovAV Has anyone been able to solve this when they aren’t using axios directly but axios is being used by another library? Sendgrid is getting this error but I don’t have access to axios through that

I have encountered the same issue when I use axios in a chrome extension background.js. And I resolved this problem.

My extension is made by forking https://github.com/antfu/vitesse-webext/tree/369fc0386970c77e35d401a2deb4d2ef71768aea .

pnpm add -D @vespaiach/axios-fetch-adapter@0.3.1 axios@0.26.1
import axios from 'axios'
import fetchAdapter from '@vespaiach/axios-fetch-adapter';

const service = axios.create({ adapter: fetchAdapter })

ref: https://zenn.dev/nicopin/articles/5dc87c27bd08de

It turns out that any request after the first request fails

Maybe your interceptors did something special to the default config. Please check for that.

@qiankunxienb @evanb2 what workaround did you applied to resolve

Also landed here after getting this error trying to use axios in a Chrome extension background file. Sad face.

Had similar issue, in my case I was using some polyfill plugin with webpack, which made the production code somehow browser like, so

Object.prototype.toString.call(process) === '[object object]'

Then I ran the code in Node, and axios can’t pick any adaptor by this code:

function getDefaultAdapter() {
  var adapter;
  if (typeof XMLHttpRequest !== 'undefined') {
    // For browsers use XHR adapter
    adapter = require('./adapters/xhr');
  } else if (typeof process !== 'undefined' && Object.prototype.toString.call(process) === '[object process]') {
    // For node use HTTP adapter
    adapter = require('./adapters/http');
  }
  return adapter;
}

not sure why we need to check Object.prototype.toString.call(process) === '[object process], isn’t typeof process !== 'undefined' enough to determine it is a Node environment?

To solve this, turns out I don’t actually need the polyfill plugin to build the production code… so I just removed that plugin from webpack, and it worked…

I kind of worked around this with this hack:

import axiosOriginal from 'axios'
import adapter from 'axios/lib/adapters/xhr'

const axios = axiosOriginal.create({ adapter })