next.js: Cannot destructure property `publicRuntimeConfig` of 'undefined' or 'null'. (v9.5.0)

Bug report

After upgrading from 9.4.4 to 9.5.0 I get an error after building (in dev mode all works fine):

Cannot destructure property `publicRuntimeConfig` of 'undefined' or 'null'.

Code:

import getConfig from "next/config";
const { publicRuntimeConfig } = getConfig()

System information

  • OS: macOS
  • Browser: chrome
  • Version of Next.js: 9.5.0

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 17
  • Comments: 16 (2 by maintainers)

Commits related to this issue

Most upvoted comments

For those who encounter this running jest, here is a fix tested In my case I was using

import getConfig from 'next/config';
const { publicRuntimeConfig } = getConfig();

Changing the above to import { publicRuntimeConfig } from '<path/to>/next.config.js'; helped

I solved this issue with this lines in jest.setup file:

import { setConfig } from 'next/config'
import config from './next.config'

setConfig(config)

Oke, so I found the situation when this is happening. I use next-transpile-modules to import modules from a different component package. But when I try to call getConfig inside a component in a different package it fails, a workaround is passing the config to the Component.

I also have next-transpile-modules in my next.config.js and running into this issue when running my tests? Any recommendations?

I am geting this error in Next 9.5.5 under jest tests, just checking if my component isDefined

I’ve got the same error too, when I’ve introduced getStaticProps. it occurred when next dev subcommand executed, but the next build && next start did not the case.

Version of Next.js: 9.5.1

I encountered this same error when using yarn workspaces with one workspace depending on next 9.4 and another 9.5. updating next to the latest version in all workspaces resolved it for me.

It seems to be related to lerna & different not being installed correctly, installing it in the root of the project with yarn add next -W fixed it for me.