developit/redaxios

ReferenceError: self is not defined

andreisocaciu opened this issue · 8 comments

I get an error when trying to use redaxios with Next.js.
Maybe it's because of SSR?

ReferenceError: self is not defined
    at e (node_modules/redaxios/dist/redaxios.js:1:1506)

Yes. Under node, use axios.

I'm in the browser, but Next.js apps are server-side rendered.

There are two solutions here:

  1. Publish redaxios with a browser field and point main at the axios package. I'm open to this and will give it a try next week.

  2. Use Webpack aliases to automatically load "redaxios" instead of "axios", but only in browser bundles:

// next.config.js
module.exports = {
  webpack(config, env) {
    if (!env.ssr) {
      config.resolve.alias.axios = 'redaxios';
    }
  }
}

Same happens for anyone who uses gatsbyjs

@kevlened apologies for the delay, version 0.3.0 is now published and includes this fix.

@barbalex in Gatsby you should never be using Redaxios on the server (or during prerendering). It must be aliased to axios, since redaxios is not a Node HTTP library.

@developit yes. And that is important to know for gatsby users that would use it server side.

Unfortunately gatsby causes redaxios to run server side while building the app, even when it is used only client side. Which will probably be the case for most gatsby users using redaxios and is so in our case.

Anyway: v0.3.0 solves the issue. Thanks a lot!