Skip to content

Storybook not resolving __dirname path #387

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
mqwebster-nava opened this issue Mar 12, 2025 · 2 comments · Fixed by #388 or #392
Closed

Storybook not resolving __dirname path #387

mqwebster-nava opened this issue Mar 12, 2025 · 2 comments · Fixed by #388 or #392
Assignees
Labels
bug Something isn't working triage New issue which needs reviewed by a maintainer

Comments

@mqwebster-nava
Copy link
Contributor

What happened?

It appears that Storybook is having issues with resolving the __dirname path in the nextConfigPath variable. This is happening when attempting to run the npm run storybook command to run Storybook in dev. The problem appears to be coming from the main.js file in the Storybook setup: template/{{app_name}}/.storybook/main.js.

This issue appears resolved when the code below is added to the top of our main.js file. However, I'm still unsure of the root cause.

import path, { dirname } from "path";
import { fileURLToPath } from "url";

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

Steps to reproduce

From the {{ APP_NAME }} directory, run the npm run storybook command from the terminal.

What browsers are you seeing the problem on?

Chrome

Relevant log output

SB_CORE-SERVER_0007 (MainFileEvaluationError): Storybook couldn't evaluate your .storybook/main.js file.

Original error:
ReferenceError: __dirname is not defined
    at file:///Users/marqueswebster/Documents/Projects/template-application-nextjs/template/%7B%7Bapp_name%7D%7D/.storybook/main.js:29:36
    at ModuleJobSync.runSync (node:internal/modules/esm/module_job:396:35)
    at ModuleLoader.importSyncForRequire (node:internal/modules/esm/loader:366:47)
    at loadESMFromCJS (node:internal/modules/cjs/loader:1565:24)
    at Module._compile (node:internal/modules/cjs/loader:1716:5)
    at Object..js (node:internal/modules/cjs/loader:1899:10)
    at Module.load (node:internal/modules/cjs/loader:1469:32)
    at Function._load (node:internal/modules/cjs/loader:1286:12)
    at TracingChannel.traceSync (node:diagnostics_channel:322:14)
    at wrapModuleLoad (node:internal/modules/cjs/loader:235:24)
    at loadMainConfig (./node_modules/@storybook/core/dist/common/index.cjs:17511:11)
    at async buildDevStandalone (./node_modules/@storybook/core/dist/core-server/index.cjs:37134:11)
    at async withTelemetry (./node_modules/@storybook/core/dist/core-server/index.cjs:35757:12)
    at async dev (./node_modules/@storybook/core/dist/cli/bin/index.cjs:2591:3)
    at async s.<anonymous> (./node_modules/@storybook/core/dist/cli/bin/index.cjs:2643:74)

WARN Broken build, fix the error above.
WARN You may need to refresh the browser.
@mqwebster-nava mqwebster-nava added bug Something isn't working triage New issue which needs reviewed by a maintainer labels Mar 12, 2025
@mqwebster-nava mqwebster-nava self-assigned this Mar 13, 2025
@jpandersen87
Copy link

The lack of the double underscore global values for filename and dirname likely means that the main.js file is being ran within an ESM module context instead of CommonJS as those values are no longer available in ESM. Considering ESM is now the standard module format, it may be best to consider necessary changes to the template to officially become ESM-compatible.

@mqwebster-nava
Copy link
Contributor Author

@jpandersen87 thanks for flagging this. FYA: I submitted this PR that should resolve it according to Node documentation.

lorenyu added a commit that referenced this issue Mar 27, 2025
mqwebster-nava added a commit that referenced this issue 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`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working triage New issue which needs reviewed by a maintainer
Projects
None yet
2 participants