I'm trying to use axios in convex
I'm trying to use axios in convex, but I'm getting:
25 Replies
I did set up convex.json as such to use external packages:
Is this running in the convex environment or did you add
"use node"
to the top of the file?this is running in convex environment, I did not add
use node
, honestly I'm not sure when I should or should not add use node
Just tried adding it:
You can read about when to use node here: https://docs.convex.dev/functions/runtimes#default-convex-runtime
It’s not required for fetch, but I’m not sure about axios. I know I had issues using a library that used axios and ended up having to use node.
Runtimes | Convex Developer Hub
Convex functions can run in two runtimes:
any idea what the error above means, which I am getting after I added
"use node"
?
Reposting for clarity:
@wjarjoui @wjarjoui XMLHttpRequest is used for browser environments, use the HTTP adapter for Convex's NodeJS environment:
const axios = require('axios');
const httpAdapter = require('axios/lib/adapters/http');
axios.defaults.adapter = httpAdapter;
I'll give that a shot
Unfortunately I'm still getting the following error:
I moved it all to
fetch
, but would be great to understand what is going on hereare there any other errors that might indicate
convex/kyc/quadrata.ts
is not compiling correctly?
Totally infering here, but it looks like convex is expecting the compiled js from that file and it isn't there, so I assume compiler error from typescript.@wjarjoui is kyc/quadrata.js your file? What libraries does it import?
Yes that is my file. It is otherwise working until I add axios. Is there a working example with axios that I can refer to?
@wjarjoui heya. so, the overarching question we have left here is, does axios work in a nodejs-based convex action? or any convex action for that matter...
correct
gotcha. I'll give it a whirl
b/c I have no idea
🙂
ty 🙂
@wjarjoui got it working
GitHub
GitHub - jamwt/axios-test: Example of axios working within the Conv...
Example of axios working within the Convex runtime - GitHub - jamwt/axios-test: Example of axios working within the Convex runtime
that's working in our runtime (no 'use node')
the main issue is axios doesn't have a built-in adapter for 'fetch'. it has one for xhr and node's http library
there was a third party one here: https://github.com/vespaiach/axios-fetch-adapter
GitHub
GitHub - vespaiach/axios-fetch-adapter: Fetch adapter for axios
Fetch adapter for axios. Contribute to vespaiach/axios-fetch-adapter development by creating an account on GitHub.
but the maintainer is behind on keeping it current and working with axios 1.X.
I created a fork with some fixes for new axios here: https://github.com/jamwt/axios-fetch-adapter
GitHub
GitHub - jamwt/axios-fetch-adapter: Fetch adapter for axios
Fetch adapter for axios. Contribute to jamwt/axios-fetch-adapter development by creating an account on GitHub.
but basically, if you use that fetch adapter, axios works just fine in our runtime
GitHub
axios-test/convex/test.ts at main · jamwt/axios-test
Example of axios working within the Convex runtime - jamwt/axios-test
this one worked with the node.js runtime as well for me: https://github.com/jamwt/axios-test/blob/main/convex/test_node.ts
GitHub
axios-test/convex/test_node.ts at main · jamwt/axios-test
Example of axios working within the Convex runtime - jamwt/axios-test
axios version 1.6.4
ok great thank you!