Skip to content

[Bug]: Node.js v24 - ReferenceError: require is not defined #31415

@valentinpalkovic

Description

@valentinpalkovic

Describe the bug

When starting Storybook with Node.js v24 and using the getAbsolutePath/wrapPnP helpers in .storybook/main.<ts|js>, Storybook crashes with the following error message:

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

Original error:
ReferenceError: require is not defined
    at getAbsolutePath (/.storybook/main.ts:11:20)
    at /.storybook/main.ts:18:9
    at ModuleJobSync.runSync (node:internal/modules/esm/module_job:455:35)
    at ModuleLoader.importSyncForRequire (node:internal/modules/esm/loader:435:47)
    at loadESMFromCJS (node:internal/modules/cjs/loader:1565:24)
    at Module._compile (node:internal/modules/cjs/loader:1716:5)
    at Object.loadTS [as .ts] (node:internal/modules/cjs/loader:1826:10)
    at Module.load (node:internal/modules/cjs/loader:1469:32)
    at Module._load (node:internal/modules/cjs/loader:1286:12)
    at TracingChannel.traceSync (node:diagnostics_channel:322:14)
    at loadMainConfig (/Users/Projects/inkline/node_modules/.pnpm/@[email protected][email protected]/node_modules/@storybook/core/dist/common/index.cjs:17550:11)
    at async buildDevStandalone (/Users/Projects/inkline/node_modules/.pnpm/@[email protected][email protected]/node_modules/@storybook/core/dist/core-server/index.cjs:37127:11)
    at async withTelemetry (/Users/Projects/inkline/node_modules/.pnpm/@[email protected][email protected]/node_modules/@storybook/core/dist/core-server/index.cjs:35750:12)
    at async dev (/Users/Projects/inkline/node_modules/.pnpm/@[email protected][email protected]/node_modules/@storybook/core/dist/cli/bin/index.cjs:2591:3)
    at async s.<anonymous> (/Users/Projects/inkline/node_modules/.pnpm/@[email protected][email protected]/node_modules/@storybook/core/dist/cli/bin/index.cjs:2643:74)

In Node.js v24, it seems that .storybook/main.ts is treated as an ESM module, which leads to CJS globals not being available anymore.

Workaround:

import { dirname, join } from 'node:path';

+ import { createRequire } from 'node:module';
+ const require = createRequire(import.meta.url);

function getAbsolutePath(value: string): any {
    return dirname(require.resolve(join(value, 'package.json')));
}

System

System:
    OS: macOS 15.4.1
    CPU: (10) arm64 Apple M1 Pro
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 24.0.0 - ~/.nvm/versions/node/v24.0.0/bin/node
    npm: 11.3.0 - ~/.nvm/versions/node/v24.0.0/bin/npm
    pnpm: 10.5.2 - ~/.nvm/versions/node/v22.14.0/bin/pnpm <----- active
  Browsers:
    Chrome: 136.0.7103.92
    Edge: 136.0.3240.50
    Safari: 18.4

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions