XMLHttpRequest is not defined in Netlify Functions
Issue Summary
XMLHttpRequest is not defined in Netlify Functions
To Reproduce
- Try to fetch content via Content API JS SDK in netlify function
I think @tryghost/content-api uses axios to send AJAX requests. Axios depends heavily on XMLHttpRequest for its functioning.
Possible alternatives can be https://github.com/developit/redaxios
Technical details:
- Ghost Version: 3.41.4
- Node Version: 12.20.1
- Browser/OS: Irrelevant
- Database: mysql
Looks like the issue is linked to browser: true rollup config option. This will result in axios dependency to be built based on assumption it is used in the browser only - with xhr always used as the adaptor
I have the same issue with ElectronJS. On the main (nodejs) thread. And axios is a dependency of the project.
Any idea?
You can maybe alias the axios module imports to redaxios in your bundler.
For my project, I changed the way I was using the content API to only run on the browser.