SDK icon indicating copy to clipboard operation
SDK copied to clipboard

XMLHttpRequest is not defined in Netlify Functions

Open pankajpatel opened this issue 4 years ago • 4 comments

Issue Summary

XMLHttpRequest is not defined in Netlify Functions

To Reproduce

  1. 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

pankajpatel avatar Feb 16 '21 22:02 pankajpatel

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

nrdcp avatar Aug 25 '21 22:08 nrdcp

I have the same issue with ElectronJS. On the main (nodejs) thread. And axios is a dependency of the project.

Any idea?

gaetandezeiraud avatar Jun 23 '22 12:06 gaetandezeiraud

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.

pankajpatel avatar Jun 23 '22 19:06 pankajpatel