facebook/fbjs

self is not defined on server

clintonwoo opened this issue · 2 comments

In my react-relay project I had to change 'self' variables in the following files to 'global' to get the Node server to run... I couldn't run a server using Relay without the change.

/react-relay/node_modules/fbjs/lib/fetch.js
/react-relay/node_modules/fbjs/node_modules/whatwg-fetch/fetch.js

Error given was:
ReferenceError: self is not defined
[1] at /node_modules/react-relay/node_modules/fbjs/node_modules/whatwg-fetch/fetch.js:4:7
[1] at Object. (/node_modules/react-relay/node_modules/fbjs/node_modules/whatwg-fetch/fetch.js:330:3)
[1] at Module._compile (module.js:460:26)
[1] at Module._extensions..js (module.js:478:10)
[1] at Object.require.extensions.(anonymous function) as .js
[1] at Module.load (module.js:355:32)
[1] at Module._load (module.js:310:12)
[1] at Function.module._load (/node_modules/piping/lib/launcher.js:32:16)
[1] at Module.require (module.js:365:17)
[1] at require (module.js:384:17)

zpao commented

cc @steveluscher who I think I can at least partially blame for this 😜

I thought we weren't going to get into a situation where fetch was actually used in node?

Hi @clintonwoo. Are you trying to execute a Relay app on the server side in Node? If you are, and you're using the default network layer that imports fetch, it will break.

github/fetch is meant to be a browser polyfill only and they have no plans to make it universal.

What you should likely do, is to create (and inject) a network layer specific for the server side. Since you're already on the server, you might not even have to make network calls – you might choose to craft a network layer that resolves your queries in-place. See the Network Layer guide for more information.

Let's close this issue here. If you want to continue the conversation about your server-side Relay use-case, please do open an issue on the Relay issue tracker!

See also: 675d80a#commitcomment-12454443