Skip to content

[#387] Update Storybook nextConfigPath to support node modules #392

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

Merged
merged 1 commit into from
Apr 14, 2025

Conversation

mqwebster-nava
Copy link
Contributor

Ticket

Resolves #387

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

Originally made an update to Storybook in #388 meant to address #387. Making these changes caused issues in platform-test-nextjs.

The steps taken to resolve are in this PR.

Testing

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 on what they recommend to use in place of __dirname.

@mqwebster-nava mqwebster-nava requested a review from lorenyu April 11, 2025 21:49
Copy link
Contributor

@lorenyu lorenyu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

beautiful thanks

@mqwebster-nava mqwebster-nava merged commit 4e2e621 into main Apr 14, 2025
1 check passed
@mqwebster-nava mqwebster-nava deleted the mqwebster/storybook-modules-update branch April 14, 2025 17:30
doshitan added a commit that referenced this pull request May 30, 2025
The container image was pinned to Node.js 20.18.1, see `Dockerfile`:
``` docker
FROM node:20.18.1-bullseye-slim AS base
```

But attempting to use 20.18.1 to build Storybook, you encounter an error:
```
{
  column: 35,
  file: '/home/doshitan/projects/platform-test-nextjs/app/.storybook/main.mjs',
  length: 11,
  line: 29,
  lineText: '      nextConfigPath: path.resolve(import.meta.dirname, "../next.config.js"),',
  namespace: '',
  suggestion: ''
}
"import.meta" is not available with the "cjs" output format and will be empty
SB_CORE-SERVER_0007 (MainFileEvaluationError): Storybook couldn't evaluate your .storybook/main.mjs file.
```

This is due to a recent change[1] that switched to using `import.meta.dirname`,
a feature of Node.js ESM loading, instead of the legacy `__dirname`. But
automatic detection and loading a file as ESM is behind a feature flag until
Node.js v20.19.0[2]. Thus breaking the Storybook build for the project's
"official" Node 20.18.1 version and container workflow.

So:
- Update Node.js version to 20.19.2
- Add specific version requirement to `package.json`, which at least will
  display a warning to folks installing packages outside the container
  environment.
- Use `package.json` specification to load the correct Node.js version in CI

Which should help keep all the different environments in sync. And the new
Node.js module behavior is the default in later versions as well, so might as
well lean into it.

[1] #392
[2] https://nodejs.org/en/blog/release/v20.19.0/#requireesm-is-now-enabled-by-default
doshitan added a commit that referenced this pull request May 30, 2025
The container image was pinned to Node.js 20.18.1, see `Dockerfile`:
``` docker
FROM node:20.18.1-bullseye-slim AS base
```

But attempting to use 20.18.1 to build Storybook, you encounter an error:
```
{
  column: 35,
  file: '/home/doshitan/projects/platform-test-nextjs/app/.storybook/main.mjs',
  length: 11,
  line: 29,
  lineText: '      nextConfigPath: path.resolve(import.meta.dirname, "../next.config.js"),',
  namespace: '',
  suggestion: ''
}
"import.meta" is not available with the "cjs" output format and will be empty
SB_CORE-SERVER_0007 (MainFileEvaluationError): Storybook couldn't evaluate your .storybook/main.mjs file.
```

This is due to a recent change[1] that switched to using `import.meta.dirname`,
a feature of Node.js ESM loading, instead of the legacy `__dirname`. But
automatic detection and loading a file as ESM is behind a feature flag until
Node.js v20.19.0[2]. Thus breaking the Storybook build for the project's
"official" Node 20.18.1 version and container workflow.

So:
- Update Node.js version to 20.19.2
- Add specific version requirement to `package.json`, which at least will
  display a warning to folks installing packages outside the container
  environment.
- Use `package.json` specification to load the correct Node.js version in CI

Which should help keep all the different environments in sync. And the new
Node.js module behavior is the default in later versions as well, so might as
well lean into it.

[1] #392
[2] https://nodejs.org/en/blog/release/v20.19.0/#requireesm-is-now-enabled-by-default
doshitan added a commit that referenced this pull request May 30, 2025
The container image was pinned to Node.js 20.18.1, see `Dockerfile`:
``` docker
FROM node:20.18.1-bullseye-slim AS base
```

But attempting to use 20.18.1 to build Storybook, you encounter an error:
```
{
  column: 35,
  file: '/home/doshitan/projects/platform-test-nextjs/app/.storybook/main.mjs',
  length: 11,
  line: 29,
  lineText: '      nextConfigPath: path.resolve(import.meta.dirname, "../next.config.js"),',
  namespace: '',
  suggestion: ''
}
"import.meta" is not available with the "cjs" output format and will be empty
SB_CORE-SERVER_0007 (MainFileEvaluationError): Storybook couldn't evaluate your .storybook/main.mjs file.
```

This is due to a recent change[1] that switched to using `import.meta.dirname`,
a feature of Node.js ESM loading, instead of the legacy `__dirname`. But
automatic detection and loading a file as ESM is behind a feature flag until
Node.js v20.19.0[2]. Thus breaking the Storybook build for the project's
"official" Node 20.18.1 version and container workflow.

So:
- Update Node.js version to 20.19.2
- Add specific version requirement to `package.json`, which at least will
  display a warning to folks installing packages outside the container
  environment.
- Use `package.json` specification to load the correct Node.js version in CI

Which should help keep all the different environments in sync. And the new
Node.js module behavior is the default in later versions as well, so might as
well lean into it.

[1] #392
[2] https://nodejs.org/en/blog/release/v20.19.0/#requireesm-is-now-enabled-by-default
doshitan added a commit that referenced this pull request May 30, 2025
The container image was pinned to Node.js 20.18.1, see `Dockerfile`:
``` docker
FROM node:20.18.1-bullseye-slim AS base
```

But attempting to use 20.18.1 to build Storybook, you encounter an
error:
```
{
  column: 35,
  file: '/home/doshitan/projects/platform-test-nextjs/app/.storybook/main.mjs',
  length: 11,
  line: 29,
  lineText: '      nextConfigPath: path.resolve(import.meta.dirname, "../next.config.js"),',
  namespace: '',
  suggestion: ''
}
"import.meta" is not available with the "cjs" output format and will be empty
SB_CORE-SERVER_0007 (MainFileEvaluationError): Storybook couldn't evaluate your .storybook/main.mjs file.
```

This is due to a recent change[1] that switched to using
`import.meta.dirname`, a feature of Node.js ESM loading, instead of the
legacy `__dirname`. But automatic detection and loading a file as ESM is
behind a feature flag until Node.js v20.19.0[2]. Thus breaking the
Storybook build for the project's "official" Node 20.18.1 version and
container workflow.

So:
- Update Node.js version to 20.19.2
- Add specific version requirement to `package.json`, which at least
will display a warning to folks installing packages outside the
container environment.
- Use `package.json` specification to load the correct Node.js version
in CI

Which should help keep all the different environments in sync. And the
new Node.js module behavior is the default in later versions as well, so
might as well lean into it.

[1] #392
[2]
https://nodejs.org/en/blog/release/v20.19.0/#requireesm-is-now-enabled-by-default
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.

Storybook not resolving __dirname path
2 participants