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.

I’m using storybook on a nextjs react project, and many of my components import next/config on runtime, but I cannot seem to make it work in storybook/webpack.

being able to override imports with mocked files in .storybook would be nice. For this particular case.

For some reason setting: config.resolve.alias['next/config'] = path.join(__dirname, 'mocks/next/config.js'); in webpack.config.js doesn’t work.

I’ve tried to set resolve and alias in webpack.config, but doing import getConfig() from 'next/config'; node_modules always takes precedence.

I can do the work if anyone has an idea why this happens.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:5

github_iconTop GitHub Comments

17reactions
oliminationcommented, Oct 11, 2019

I have used the following, which worked for me:

// .storybook/config.js file
....
import { setConfig } from 'next/config';

const {
	MY_ENV_CONFIG
} = process.env;

// Set the NextJS publicRuntimeConfig property
setConfig({
	publicRuntimeConfig: {
		myEnvConfig: MY_ENV_CONFIG,
	},
});

Hope this helps.

10reactions
jmayergitcommented, Jun 2, 2020

For those using a more recent version of Storybook the above solution goes in .storybook/preview.js

Read more comments on GitHub >

github_iconTop Results From Across the Web

Next.JS & JEST - publicRuntimeConfig undefined
I've tried using jest mock in test case file. jest.mock('next/config', () => () => ({ publicRuntimeConfig ...
Read more >
publicRuntimeConfig undefined in test environment with Jest.
In my case, I mocked the next/config as shown: // jest.setup.js jest.mock('next/config' ...
Read more >
Testing | Next.js
... app to load next.config.js and .env files in your test environment dir: '. ... Create the mock files referenced in the configuration...
Read more >
Telerik React Report Viewer not working in NextJS
The NextJS application cannot render the page with the React Report Viewer . Instead of the report viewer, the page shows a stack...
Read more >
Next.JS & JEST - publicRuntimeConfig undefined-Reactjs
It works for me calling jest.mock before the Component Test Case like you mentioned: jest.mock('next/config', () => () => ({ publicRuntimeConfig: ...
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