javascript - Next.js Cannot destructure property 'publicRuntimeConfig' of '(0 , config_1.default)(...)&a

import getConfig from 'nextconfig';const { publicRuntimeConfig } = getConfig();getConfig alw

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

getConfig always returns undefined after updating from Next 10.0.7 to 10.0.8 or any other version above. But everything works with any other version below 10.0.8

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

getConfig always returns undefined after updating from Next 10.0.7 to 10.0.8 or any other version above. But everything works with any other version below 10.0.8

Share Improve this question edited Jul 7, 2022 at 8:21 adiga 35.3k9 gold badges65 silver badges87 bronze badges asked Jul 7, 2022 at 8:20 AntonAnton 311 silver badge3 bronze badges 5
  • Are you using getInitialProps or getServerSideProps for that page where you are trying to access it? And in which part of the you are using it, server or client? – Danila Commented Jul 7, 2022 at 9:07
  • We have this function below which is stored in mon folder to be used for both client and server, and it fails when it’s called on the server. And also we have getInitialProps in our _app.tsx file to opt-out of Automatic Static Optimization export const getShopConfig = () => { const { publicRuntimeConfig } = getConfig(); return publicRuntimeConfig.shopConfig; }; – Anton Commented Jul 7, 2022 at 9:58
  • 1 I'm afraid without a reproduction it will impossible to help, sounds like you do everything correctly. Try to make some minimal example which reproduces the issue. Like start a new empty Next.js app, see if it reproducible there? If not, then add some parts of your code and track when it starts to happen – Danila Commented Jul 7, 2022 at 10:18
  • This happens for me on running jest tests – HalfWebDev Commented May 8, 2023 at 13:25
  • Found any solutions? – Tomas Vancoillie Commented Nov 7, 2023 at 17:51
Add a ment  | 

3 Answers 3

Reset to default 2

In Jest, you can simply mock this using this code.

jest.mock("next/config", () => () => ({
  publicRuntimeConfig: {
    contextPath: "",
  },
}));

According to the docs the feature is deprecated.

This feature is deprecated. We remend using environment variables instead, which also can support reading runtime values.

You can use an environment variable with the prefix NEXT_PUBLIC_ and simply use process.env.NEXT_PUBLIC_MY_VARIABLE wherever you need it in the code.

i was having the same issue because the getConfig() is depreciated

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745582458a4634335.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信