Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

XMLHttpRequest is not defined #146

Closed
dennismamedov opened this issue Nov 30, 2020 · 1 comment
Closed

XMLHttpRequest is not defined #146

dennismamedov opened this issue Nov 30, 2020 · 1 comment

Comments

@dennismamedov
Copy link

Hello, I'm using Next.JS for a project. when using unfetch I get this weird error.

import fetch from 'unfetch'
...
await fetch(`/api/...`).then(res => { ... }

This piece of code errors

ReferenceError: XMLHttpRequest is not defined
    at C:\static\st-client\node_modules\unfetch\dist\unfetch.js:1:77
    at new Promise (<anonymous>)
    at module.exports (C:\static\st-client\node_modules\unfetch\dist\unfetch.js:1:45)
    at getServerSideProps (C:\static\st-client\.next\server\pages\games.js:4214:55)
    at renderToHTML (C:\static\st-client\node_modules\next\dist\next-server\server\render.js:39:221)
    at async C:\static\st-client\node_modules\next\dist\next-server\server\next-server.js:98:97
    at async C:\static\st-client\node_modules\next\dist\next-server\server\next-server.js:91:142
    at async DevServer.renderToHTMLWithComponents (C:\static\st-client\node_modules\next\dist\next-server\server\next-server.js:123:387)
    at async DevServer.renderToHTML (C:\static\st-client\node_modules\next\dist\next-server\server\next-server.js:124:473)
    at async DevServer.renderToHTML (C:\static\st-client\node_modules\next\dist\server\next-dev-server.js:34:578)

I have installed xmlhttprequest.

Any suggestions?

@developit
Copy link
Owner

developit commented Dec 2, 2020

Hi there - Next.js already polyfills fetch so there's no reason to use Unfetch.

The reason this isn't working is because Next.js is executing your code in Node in addition to the browser. For that you need to use isomorphic-unfetch, not unfetch.

Feel free to reopen of I missed something.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants