question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Incompatibility with SSR/static site builders [gatsby]

See original GitHub issue

Context

colyseus.js does not work with Gatsby. Upon trying to build, the build will fail.

The most curious thing is, this happens even by just importing the Client class, without actually instantiating or executing any colyseus.js code…

import { Client } from "colyseus.js"
...
const client = Client;  // inside React render function, executes at runtime

These two lines will cause the build to fail. The second line is there only to prevent the import from being left out by tree-shaking, it merely assigns a reference to the class.

ERROR #98123  WEBPACK
Generating SSR bundle failed
Can't import the named export 'globalAgent' from non EcmaScript module (only default export is available)
File: node_modules/httpie/dist/index.mjs

ERROR #98123  WEBPACK
Generating SSR bundle failed
Can't import the named export 'parse' from non EcmaScript module (only default export is available)
File: node_modules/httpie/dist/index.mjs

ERROR #98123  WEBPACK
Generating SSR bundle failed
Can't import the named export 'request' from non EcmaScript module (only default export is available)
File: node_modules/httpie/dist/index.mjs

ERROR #98123  WEBPACK
Generating SSR bundle failed
Can't import the named export 'resolve' from non EcmaScript module (only default export is available)
File: node_modules/httpie/dist/index.mjs

not finished Building static HTML for pages - 1.532s

Reproduction

This is a minimal Gatsby non-typescript starter, with colyseus.js as a dependency, and a single import and reference to the Client class on the main page (index.js).

Edit great-wood-3bnw4

Workaround

It’s possible to patch the WebPack config to resolve colyseus.js as null during SSR, as described in Gatsby’s docs.

exports.onCreateWebpackConfig = ({ stage, loaders, actions }) => {
  if (stage === "build-html") {
    actions.setWebpackConfig({
      module: {
        rules: [
          {
            test: /colyseus\.js/, // crashes build on import
            use: loaders.null(),
          },
        ],
      },
    })
  }
}

This fixes the error caused by importing during SSR, but will still include the library in the final browser bundle, benefitting from intelligent pre-fetching, static builds, etc. The only catch is that any colyseus-related code (such as accessing the Client class) must be used in useEffect hooks which defer to browser execution.

I know that Colyseus is not designed for SSR etc… But I moved to Gatsby to overcome some brick wall limitations of the Create-React-App, after getting frustrated by long custom WebPack configs, and parcel failing miserably.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
lukeedcommented, May 24, 2020

This error message shows that the Node.js version of httpie is being loaded. The ESM format is being understood just fine, but it can’t find the exports from http or path because those modules don’t exist.

The quick fix is to configure Gatsby so that the “browser” entry is resolved with priority, or to add an alias directly to the httpie file if you don’t want “browser” entries globally for some reason.

Colyseus can also directly import the browser version of httpie if it doesn’t intend on being universal.

1reaction
endelcommented, Oct 20, 2020

I believe this has been fixed on the latest version! Feel free to re-open if this is still breaking for you!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Troubleshooting Common Errors - Gatsby
If you encounter a webpack error that says Generating SSR bundle failed after installing a plugin and trying to run gatsby develop or...
Read more >
Build a Static Blog using Gatsby and Strapi
Publishing a static website is easy: the files are uploaded on a simple Web server or storage provider. The two main advantages of...
Read more >
Before building your next static site with React, consider this
So I just finished building my shiny new personal site ✨ I thought about using fancy, component-driven frameworks like Gatsby to stay on...
Read more >
Gatsby: The Definitive Guide [Book] - O'Reilly
Gatsby is an open source framework for building static sites, based on React. ... /static |-- gatsby-config.js |-- gatsby-node.js |-- gatsby-ssr.js ...
Read more >
Gatsby Changelog | 5.3.0
When you saw the error "Building static HTML failed for path . ... Remove exports in Gatsby files before compiling SSR/DSG engines, via...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found