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.

@emotion/server crash on import in Worker environment

See original GitHub issue

Current behavior:

Looks like @emotion/server always assumes a Node.js environment. However, it is now possible to do SSR in Workers environment (such as Cloudflare Workers). This package breaks on import because some dependencies (html-tokenize) try to access Buffer, Stream, and other Node-only APIs.

I’ve tried to get some of the problems fixed: https://github.com/LinusU/buffer-from/issues/13 However, html-tokenize have other issues.

To reproduce:

Run import createEmotionServer from '@emotion/server/create-instance' in a worker or a browser environment.

It cannot be reproduced in Codesandbox because looks like they add some Node API polyfills (Buffer, etc).

Expected behavior:

It should not crash on import. More specifically, it would be great if there was an ESM build or a way to import only the necessary functions. For example, importing createExtractCriticalToChunks and createConstructStyleTagsFromChunks, and leaving out createRenderStylesToStream, which is the one causing problems and not needed for this use case.

Right now I have this code, which works in Node but not in workers.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:11
  • Comments:12 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
Andaristcommented, Nov 24, 2022

I’d prefer not to mix ESM with require - it’s a can of worms that I prefer closed ;p

1reaction
Andaristcommented, Nov 24, 2022

Maybe the simplest solution would be to convert these imports to dynamic import() expressions inside createRenderStylesToNodeStream():

We probably can’t do that because this would taint the whole function - it would have to become async and that would be a breaking change.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Module not found: Can't resolve '@emotion/react'
I'm looking at the docs, the package you're trying to use may not exist. Make sure you are importing from the current library....
Read more >
Server Side Rendering - Emotion
When using @emotion/css ; '@emotion/react' import { renderToString } ; 'react-dom/server' import createEmotionServer ; '@emotion/server/create-instance' import ...
Read more >
Elad Shaham (@eshaham) / Twitter
@emotion/server crash on import in Worker environment · Issue #2446 · emotion-js/emotion. Current behavior: Looks like @emotion/server always assumes a ...
Read more >
emotion - Bountysource
Looks like @emotion/server always assumes a Node.js environment. However, it is now possible to do SSR in Workers environment (such as Cloudflare Workers)....
Read more >
@mui/system | Yarn - Package Manager
-import { styled, experimental_sx } from '@mui/material/styles'; +import { styled } ... ​[docs] Fix crash due to using wrong variable (#34171) @siriwatknp ...
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