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.

TypeError: Cannot read property 'key' of null on SSR

See original GitHub issue

Current behavior:

Using the css property works on the client, but on SSR gives this error:

Encountered error "#<ExecJS::ProgramError: TypeError: Cannot read property 'key' of null>" when prerendering HelloWorld with {"greeting":"Hello from react-rails."}
insertStyles ((execjs):2165:25)

Related issue: https://github.com/emotion-js/emotion/issues/1185

To reproduce:

The app I’m integrating this in is a Rails app using https://github.com/reactjs/react-rails so I made a repro with it. Hopefully you have ruby installed 🤞.

git clone git@github.com:ksweetie/emotion-ssr.git
cd emotion-ssr/
bundle
yarn
rails s
// visit localhost:3000/foo/foo

Here’s a list of files I changed, otherwise it’s a stock Rails app. Changing prerender: true to false in foo.html.erb should show that it’s working on the client.

app/views/foo/foo.html.erb
app/javascript/components/HelloWorld.tsx
config/webpack/loaders/typescript.js
tsconfig.json
babel.config.js

Expected behavior:

Turning on SSR doesn’t give an error.

Environment information:

  • react version: 16.12.0
  • emotion version: 10.0.27

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:18 (6 by maintainers)

github_iconTop GitHub Comments

5reactions
KevinGhadyani-mintedcommented, Mar 18, 2020

I was using emotion v10.0.9, and it was working fine. Once I went to v10.0.27, that’s when the issue occurred.

The issue is that cache is null in render inside of @emotion/core. So when it calls utils.getRegisteredStyles and utils.insertStyles, it breaks.

The codebase I’m working with is using Enzyme. If I mount, it breaks the test in the same way as the original poster. All css props activate without a cache provider, and it’s not creating one.

I believe it’s because of this:

var EmotionCacheContext = React.createContext( // we're doing this to avoid preconstruct's dead code elimination in this one case
// because this module is primarily intended for the browser and node
// but it's also required in react native and similar environments sometimes
// and we could have a special build just for that
// but this is much easier and the native packages
// might use a different theme context in the future anyway
typeof HTMLElement !== 'undefined' ? createCache() : null);

Specifically typeof HTMLElement !== 'undefined'. It’s not auto-creating a cache when that happens.

2reactions
Andaristcommented, Mar 27, 2020

@lucat1 this particular problem happens because .10 and .12 prereleases got mixed there and there were some breaking changes in between them, so older version of @emotion/css has been crashing while using newer @emotion/cache. I’ve prepared a PR to fix this: https://github.com/zeit/next.js/pull/11414

Read more comments on GitHub >

github_iconTop Results From Across the Web

getting error "TypeError: Cannot read properties of null ...
@BraceSproul I have this error and I AM importing react . I'm using the latest nextjs, react and typescript. – Kirk Ross. Dec...
Read more >
cannot read properties of null (reading 'trim') - You.com
You're getting error. Uncaught TypeError: Cannot read property 'trim' of undefined in Jquery. that means, the variable vname is undefined .
Read more >
How to Avoid the Infamous "Cannot read properties of ... - Bitovi
As a JavaScript developer, I'm sure you've encountered the frustrating runtime TypeError Cannot read properties of undefined .
Read more >
Cannot read property 'medium' of null" Cant figure it out! - Reddit
I keep getting a "TypeError: Cannot read property 'medium' of null" particular in my second component that contains an img key from an...
Read more >
cannot read property of undefined redux - Retro Remix Advance
DesignUncaught TypeError: Cannot read properties of undefined (reading 'remove') ... Render Error: 'TypeError: Cannot read property [propertyName] of null'.
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