wjarjoui
wjarjoui2y ago

I'm trying to use axios in convex

I'm trying to use axios in convex, but I'm getting:
Error: Uncaught AxiosError: Adapter xhr is not supported by the environment
at getAdapter [as getAdapter] (../../node_modules/axios/lib/adapters/adapters.js:39:10)
at dispatchRequest (../../node_modules/axios/lib/core/dispatchRequest.js:50:30)
at request [as request] (../../node_modules/axios/lib/core/Axios.js:146:6)
at httpMethod (../../node_modules/axios/lib/core/Axios.js:185:8)
at wrap (../../node_modules/axios/lib/helpers/bind.js:3:9)
at getAccessToken (../../convex/kyc/quadrata.ts:46:12)
at handler (../../convex/kyc/quadrata.ts:65:27)

at ConvexHttpClient.action (/home/node/app/node_modules/convex/dist/cjs/browser/http_client.js:253:15)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async WalletService.updateWalletDocumentsFromQuadrata (/home/node/app/dist/src/wallet/services/wallet.service.js:54:22)
at async WalletService.getWallet (/home/node/app/dist/src/wallet/services/wallet.service.js:153:20)
at async WalletService.getWalletInfo (/home/node/app/dist/src/wallet/services/wallet.service.js:44:28)
at async WalletController.getWalletInfo (/home/node/app/dist/src/wallet/wallet.controller.js:27:16)
at async /home/node/app/node_modules/@nestjs/core/router/router-execution-context.js:46:28
at async /home/node/app/node_modules/@nestjs/core/router/router-proxy.js:9:17
Error: Uncaught AxiosError: Adapter xhr is not supported by the environment
at getAdapter [as getAdapter] (../../node_modules/axios/lib/adapters/adapters.js:39:10)
at dispatchRequest (../../node_modules/axios/lib/core/dispatchRequest.js:50:30)
at request [as request] (../../node_modules/axios/lib/core/Axios.js:146:6)
at httpMethod (../../node_modules/axios/lib/core/Axios.js:185:8)
at wrap (../../node_modules/axios/lib/helpers/bind.js:3:9)
at getAccessToken (../../convex/kyc/quadrata.ts:46:12)
at handler (../../convex/kyc/quadrata.ts:65:27)

at ConvexHttpClient.action (/home/node/app/node_modules/convex/dist/cjs/browser/http_client.js:253:15)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async WalletService.updateWalletDocumentsFromQuadrata (/home/node/app/dist/src/wallet/services/wallet.service.js:54:22)
at async WalletService.getWallet (/home/node/app/dist/src/wallet/services/wallet.service.js:153:20)
at async WalletService.getWalletInfo (/home/node/app/dist/src/wallet/services/wallet.service.js:44:28)
at async WalletController.getWalletInfo (/home/node/app/dist/src/wallet/wallet.controller.js:27:16)
at async /home/node/app/node_modules/@nestjs/core/router/router-execution-context.js:46:28
at async /home/node/app/node_modules/@nestjs/core/router/router-proxy.js:9:17
25 Replies
wjarjoui
wjarjouiOP2y ago
I did set up convex.json as such to use external packages:
{
"node": {
"externalPackages": ["*"]
}
}
{
"node": {
"externalPackages": ["*"]
}
}
erquhart
erquhart2y ago
Is this running in the convex environment or did you add "use node" to the top of the file?
wjarjoui
wjarjouiOP2y ago
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:
400 Bad Request: InvalidModules: Hit an error while pushing:
Loading the pushed modules encountered the following
error:
Uncaught Failed to analyze kyc/quadrata.js: Invalid URL
400 Bad Request: InvalidModules: Hit an error while pushing:
Loading the pushed modules encountered the following
error:
Uncaught Failed to analyze kyc/quadrata.js: Invalid URL
erquhart
erquhart2y ago
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:
wjarjoui
wjarjouiOP2y ago
any idea what the error above means, which I am getting after I added "use node"? Reposting for clarity:
400 Bad Request: InvalidModules: Hit an error while pushing:
Loading the pushed modules encountered the following
error:
Uncaught Failed to analyze kyc/quadrata.js: Invalid URL
400 Bad Request: InvalidModules: Hit an error while pushing:
Loading the pushed modules encountered the following
error:
Uncaught Failed to analyze kyc/quadrata.js: Invalid URL
Omar
Omar2y ago
@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;
wjarjoui
wjarjouiOP2y ago
I'll give that a shot Unfortunately I'm still getting the following error:
400 Bad Request: InvalidModules: Hit an error while pushing:
Loading the pushed modules encountered the following
error:
Uncaught Failed to analyze kyc/quadrata.js: Invalid URL
400 Bad Request: InvalidModules: Hit an error while pushing:
Loading the pushed modules encountered the following
error:
Uncaught Failed to analyze kyc/quadrata.js: Invalid URL
I moved it all to fetch, but would be great to understand what is going on here
erquhart
erquhart2y ago
are 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.
ballingt
ballingt2y ago
@wjarjoui is kyc/quadrata.js your file? What libraries does it import?
wjarjoui
wjarjouiOP2y ago
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?
jamwt
jamwt16mo ago
@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...
wjarjoui
wjarjouiOP16mo ago
correct
jamwt
jamwt16mo ago
gotcha. I'll give it a whirl b/c I have no idea 🙂
wjarjoui
wjarjouiOP16mo ago
ty 🙂
jamwt
jamwt16mo ago
@wjarjoui got it working
jamwt
jamwt16mo ago
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
jamwt
jamwt16mo ago
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
jamwt
jamwt16mo ago
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.
jamwt
jamwt16mo ago
but the maintainer is behind on keeping it current and working with axios 1.X.
jamwt
jamwt16mo ago
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.
jamwt
jamwt16mo ago
but basically, if you use that fetch adapter, axios works just fine in our runtime
jamwt
jamwt16mo ago
jamwt
jamwt16mo ago
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
jamwt
jamwt16mo ago
axios version 1.6.4
wjarjoui
wjarjouiOP16mo ago
ok great thank you!

Did you find this page helpful?