1

I'm trying to prerender my react app server side but I came accross a problem: as soon as I require('algoliasearch') I get ReferenceError: XMLHttpRequest is not defined

I tried to use faux-jax to mock XMLHttpRequest but then I get TypeError: Cannot read property '0' of undefined And after all I don't think that's the way to go.

Any idea on this problem?

Shipow
  • 2,451
  • 1
  • 21
  • 28
Nima Izadi
  • 996
  • 6
  • 18

1 Answers1

1

are you using nodejs? If so which version?

When using nodejs 0.12 I cannot reproduce your issue:

> node -v
0.12.13
> npm install algoliasearch
node
> var algoliasearch = require('algoliasearch');
undefined
> algoliasearch
{ [Function: algoliasearch] version: '3.4.0', ua: 'Algolia for Node.js 3.4.0' }
vvo
  • 2,653
  • 23
  • 30
  • Hum, I had `0.10` but updated to `0.12` and still have the error. It seems that the error is related to React-Rails or ExecJS and not node itself. As soon as I remove `require('algoliasearch')` from my react app, the rendering works... Could it be related to `algoliasearch` dependencies? – Nima Izadi Jun 02 '15 at 13:23
  • Can you do exactly the lines I did and let me know if it works? – vvo Jun 02 '15 at 13:29
  • Right, I forgot to mention that I did the line you did and it worked. – Nima Izadi Jun 02 '15 at 13:30
  • The weird thing is that at no point you should get "XMLHttpRequest is not defined". This code is only loaded when used with browserify or webpack. I do not know what is execJS but yes it seems more related to execJS than to node or react. – vvo Jun 02 '15 at 13:31
  • There seems to be incompatibilities between react rails and browserify https://github.com/reactjs/react-rails/issues/129 – vvo Jun 02 '15 at 13:31
  • It could be related to the package.json of the algoliasearch module but I really do not know what is hapening. It seems there's someone trying to load `src/browser/builds/algoliasearch.js` server side while it should just load index.js – vvo Jun 02 '15 at 13:34