axios: Axios doesn't work on edge runtime, Adapter 'http' is not available in the build
Describe the bug
I am building a library that uses axios, when I run it on Serverless edge functions (e.g: on vercel) it fails with error:
Error: Adapter 'http' is not available in the build
at Object.getAdapter (webpack-internal:///(middleware)/../../sdks/node/node_modules/axios/dist/browser/axios.cjs:2371:13)
at Axios.dispatchRequest (webpack-internal:///(middleware)/../../sdks/node/node_modules/axios/dist/browser/axios.cjs:2426:28)
It seems that it doesn’t pick any adapter when running on edge. I tried to manually set adapter to http but I got the same error.
I know fetch is supported on edge runtime, so I tried with a custom adapter, its not completed, still struggling with setting the headers and query params but it works and doesn’t generate errors:
axiosOptions.adapter = function (config) {
return new Promise(async (resolve, reject) => {
try {
const reqOptions: RequestInit = {
method: config.method,
keepalive: true,
body: config.data,
}
const resp = await fetch("some url", reqOptions);
const payload = await resp.text();
const response: AxiosResponse = {
data: payload,
status: resp.status,
statusText: resp.statusText,
headers: {
'content-type': resp.headers.get('content-type') || undefined,
},
config: config,
};
resolve(response);
} catch (err: any) {
console.log({ err });
const axiosErr = new AxiosError(err.message);
reject(axiosErr);
}
});
};
I don’t know it the http
adapter could work on edge, is there a way to force axios to include it? If not, can we add a fetch adapter to the lib?
When I run axios without creating an instance the errors differs a bit:
Error [TypeError]: adapter is not a function
at dispatchRequest (webpack-internal:///(middleware)/./node_modules/axios/lib/core/dispatchRequest.js:58:10)
at Axios.request (webpack-internal:///(middleware)/./node_modules/axios/lib/core/Axios.js:109:15)
at Axios.<computed> [as get] (webpack-internal:///(middleware)/./node_modules/axios/lib/core/Axios.js:131:17)
at Function.wrap [as get] (webpack-internal:///(middleware)/./node_modules/axios/lib/helpers/bind.js:9:15)
at Object.handler (webpack-internal:///(middleware)/./pages/api/axios.js:11:69)
at adapter (webpack-internal:///(middleware)/./node_modules/next/dist/server/web/adapter.js:60:33)
at Module.__WEBPACK_DEFAULT_EXPORT__ (webpack-internal:///(middleware)/./node_modules/next/dist/build/webpack/loaders/next-edge-function-loader.js?absolutePagePath=%2FUsers%2Fshehata%2Faxiom%2Ftesting-projects%2Faxiom-local-next%2Fpages%2Fapi%2Faxios.js&page=%2Fapi%2Faxios!:24:87)
To Reproduce
create a nextjs project and setup an edge function under pages/api/
.
run on vercel platform, will run in Edge runtime.
Code snippet
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
import axios from 'axios'
async function handler(req, _ev) {
await axios.get('https://google.com')
return new Response('ok')
}
export const config = {
runtime: 'experimental-edge',
};
export default handler;
Expected behavior
No response
Axios Version
1.2.0
Adapter Version
No response
Browser
No response
Browser Version
No response
Node.js Version
16.15.0
OS
OSX
Additional Library Versions
No response
Additional context/Screenshots
No response
About this issue
- Original URL
- State: open
- Created a year ago
- Reactions: 19
- Comments: 16
Commits related to this issue
- Migrate layout and index page to the app router Add app directory and define layout and page Move index page to app/home-page and remove getStaticProps Use Edge runtime to fix build errors when bac... — committed to merpw/forum by maximihajlov a year ago
- Migrate layout and index page to the app router Add app directory and define layout and page Move index page to app/home-page and remove getStaticProps Use Edge runtime to fix build errors when bac... — committed to merpw/forum by maximihajlov a year ago
- Features/frontend app directory (#54) Migrate the frontend to the new Next.js app directory router * Update dependencies to the version where app dir is stable * Migrate layout and index page t... — committed to merpw/forum by maximihajlov a year ago
- fix: axios not working on edge runtime https://github.com/axios/axios/issues/5523 — committed to ali4heydari/ali4heydari.github.io by ali4heydari 9 months ago
- Remove axios (#18) Switch to using (isomorphic) fetch. This requires node >= 17, but we haven't tested on earlier versions (and doubt we support them). With this change, the SDK should work just ... — committed to braintrustdata/braintrust-sdk by ankrgyl 8 months ago
- feat: remove axios cause it don't run on the edge https://github.com/axios/axios/issues/5523 — committed to room302studio/npm-search-viz by ejfox 7 months ago
- fix: Fixed problems regarding the crypto API and axios - Since axios is not supporting the edge runtime, I deleted axios. See more here: https://github.com/axios/axios/issues/5523 - I also switched f... — committed to The-Creative-Programming-Group/Weather-App by FleetAdmiralJakob 5 months ago
We have noticed this bug in our project, too. With both Microsoft Edge and Google Chrome. People on stackoverflow also talk about it: https://stackoverflow.com/questions/75280544/uncaught-in-promise-error-error-adapter-http-is-not-available-in-the-build .
Workaround
It seems there is no easy workaround, but you can use fetch() in case you are developing browser extensions.
Yeah, I just migrate to xior
Almost same API with axios, plugins support, based on fetch, the size very lite too.
Default adapter setting:
{adapter: ['xhr', 'http']}
Axios is trying to use the first adapter available, so if you’re getting this error it means your environment doesn’t support bothxhr
andhttp
adapters. Currently, the Axios adapter loader is fairly minimalistic, as a decent implementation requires other depend tasks to be done beforehand, so it can only display the latest adapter load error. If you set{adapter: 'xhr'}
you will get an error aboutxhr
only. In practice, this meansXMLHttpRequest
API is not available.That axios is not working on edge workers like Deno Deploy, Vercel Edge Functions or Cloudflare Workers is an absolute dealbreaker for me.
Also having issue with Axios in middleware. I think fetch is the way to go for any requests happening in middleware