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 '_documentProps' of null

See original GitHub issue

Bug report

Describe the bug

TypeError: Cannot read property '_documentProps' of null

This gets thrown in the render method of Head :

render() {
    var {
      styles,
      ampPath,
      inAmpMode,
      assetPrefix,
      hybridAmp,
      canonicalBase,
      __NEXT_DATA__,
      dangerousAsPath,
      headTags
    } = this.context._documentProps;

Works fine in development but when building and running the build version it crashes

To Reproduce

Steps to reproduce the behavior, please provide code snippets or a repository:

I understand this might be something related to my setup. I am using typescript and nextjs.

Expected behavior

No error thrown.

System information

  • OS: macOS
  • Browser (if applies) chrome (any)
  • Version of Next.js: 9.1.4

Additional context

App works fine in 9.0.4 but I have a depenency that is throwing a vulnerability warning…and shouldn’t be a big update (I assumed).

The issue alwways shows itself in the first element inside the Document in pages/_document. In this case its “Head” but if I remove Head then it shows itself in “Main”. Somehow this context is not getting populated…only when building (works in development)

This is the getInitialProps of my _document page :

public static async getInitialProps(context: CustomDocumentContext) {
    const sheet = new ServerStyleSheet();
    const renderPage = context.renderPage;

    try {
      context.renderPage = () => {
        const enhancer = App => props => {
          return sheet.collectStyles(<App {...props} />);
        };

        return renderPage(enhancer);
      };

      const initialProps: any = await BaseDocument.getInitialProps(context);
      const { language, locale, localeData } = context.req;

      return {
        ...initialProps,
        language,
        locale,
        localeData,
        styles: (
          <>
            {initialProps.styles}
            {sheet.getStyleElement()}
          </>
        ),
      };
    } finally {
      // @ts-ignore
      sheet.seal();
    }
  }

Issue Analytics

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

github_iconTop GitHub Comments

278reactions
phoenisxcommented, Feb 9, 2020

I faced the same issue, not sure if this is related. But, the issue was caused due to improper import.

Instead of import { Head } from 'next/document', it should be import Head from 'next/head', if anybody is trying to import Head inside individual pages, and not in _document, as stated in docs.

6reactions
andresmejia3commented, Mar 6, 2020

@Shub1427 You’re a beauty, thanks!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Next.js 11: Cannot read property 'canonicalBase' of undefined
Is there a standard function to check for null, undefined, or blank variables in JavaScript? 434 · React - uncaught TypeError: Cannot read...
Read more >
cannot read property 'usecontext' of null nextjs - You.com
This gives the error: TypeError: Cannot read property 'pathname' of null. The call stack shows: getInitialProps pages\marketplace\index.js (73:46).
Read more >
typeerror can not read property x of null - YouTube
Join this channel to get access to perks:https://www.youtube.com/channel/UCoSpmr2KNOxjwE_B9ynUmig/joinMy GearCamera ...
Read more >
TypeError: Cannot read properties of null (reading 'type')
[This thread is closed.] When we try accessing the plugin settings we are getting the below error. We tried re-installing the plugin multiple...
Read more >
JavaScript TypeError Cannot Read Property 'style' of Null
Learn how to deal with a “cannot read property 'style' of null” error in JavaScript with this article by Career Karma.
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