Skip to content

Resolve Storybook build error #98

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 8 commits into from

Conversation

mqwebster-nava
Copy link
Contributor

@mqwebster-nava mqwebster-nava commented Apr 8, 2025

Ticket

Resolves Deploy Storybook Actions step

Changes

  • Updated .storybook/main.js => .storybook/main.mjs based on ECMAScript Modules instead of CommonJS
    • .storybook/main.mjs:
      path.resolve(import.meta.dirname, "../next.config.js")
      

Context for reviewers

Storybook was having issues with building and running in the template-application-nextjs repo. The particular error was about .storybook/main.js having problems with resolving the __dirname path in the nextConfigPath variable. This issue comes from some changes in how Storybook and Node.js handle JavaScript files/modules. Storybook is having issues with using CommonJS, so the apparent solution at the time was to update the .storybook/main.js config file to support Node.js ESM. More can be found here: template-application-next.js: issue #387.

After pushing this change to main in that repo, it has caused some issues in the deployment pipeline of this one. The CI/CD pipeline failed at the Deploy Storybook step. Based on the error, the exact cause is a bit unclear because it gives the error ReferenceError: require is not defined which is a little confusing because .storybook/main.js does not use this node module. It appears that this issue may be bubbling up from the next.config.js file as this is the only file referenced in .storybook/main.js and it does use the require module. The next.config.js will need to be updated to support ESM instead of CommonJS Node modules.

--- Update ---

I believe I may have been over-engineering the solution to this problem. Rather than making changes to the .storybook/main.js AND next.config.js file, I made changes to the .storybook/main.js file only. I removed the changes I originally made in favor of using import.meta.dirname in place of __dirname based on Node.js docs. I also changed the file type from .js to .mjs to ensure this file is strictly using ESModules instead of CommonJS. This appears to resolve all build steps instead of the app vulnerability check.

Testing

I've gotten this to a point where the CI/CD pipeline actions are able to run successfully except for the vulnerability check. Storybook and the rest of the app is successfully able to build after making this change to .storybook/main.js.

Preview environment for app

♻️ Environment destroyed ♻️

Copy link

github-actions bot commented Apr 8, 2025

Coverage report for app

St.
Category Percentage Covered / Total
🟢 Statements 97.92% 47/48
🟢 Branches 85.71% 6/7
🟢 Functions 100% 8/8
🟢 Lines 100% 40/40

Test suite run success

5 tests passing in 3 suites.

Report generated by 🧪jest coverage report action from 1b79c6e

@lorenyu lorenyu self-requested a review April 11, 2025 20:53
@lorenyu
Copy link
Contributor

lorenyu commented Apr 11, 2025

@mqwebster-nava this looks great, now that this is tested, can you copy over the changes to https://github.com/navapbc/template-application-nextjs , create a PR, tag me for review, and we can merge it from there?

@mqwebster-nava
Copy link
Contributor Author

@lorenyu will do!

mqwebster-nava added a commit to navapbc/template-application-nextjs that referenced this pull request Apr 14, 2025
## Ticket

Resolves #387 

## Changes

<!-- What was added, updated, or removed in this PR. -->
- Updated .storybook/main.js => .storybook/main.mjs based on ECMAScript
Modules instead of CommonJS
  - **.storybook/main.mjs:**
    ```    
    path.resolve(import.meta.dirname, "../next.config.js")
    ```

## Context for reviewers

<!-- Testing instructions, background context, more in-depth details of
the implementation, and anything else you'd like to call out or ask
reviewers. -->
Originally made an update to Storybook in #388 meant to address #387.
Making these changes caused issues in
[platform-test-nextjs](https://github.com/navapbc/platform-test-nextjs/actions/runs/13999633004/job/39317883628).

The steps taken to resolve are in this
[PR](navapbc/platform-test-nextjs#98).

## Testing

<!-- Provide evidence that the code works as expected. Explain what was
done for testing and the results of the test plan. Include screenshots,
[GIF demos](https://getkap.co/), shell commands or output to help show
the changes working as expected. ProTip: you can drag and drop or paste
images into this textbox. -->
Making this change in platform-test-nextjs resulted in a successful app
build (save for a vulnerability scan). This change also follows [Node.js
documentation](https://nodejs.org/docs/latest/api/esm.html#no-__filename-or-__dirname)
on what they recommend to use in place of `__dirname`.
@mqwebster-nava
Copy link
Contributor Author

Resolved in navapbc/template-application-nextjs#392

@mqwebster-nava mqwebster-nava deleted the mqwebster/storybook-build-error branch April 14, 2025 18:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants