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.

Webpack5 + type: module = require is not defined

See original GitHub issue

Describe the bug A barebones example with type: module and Webpack5 results in “require is not defined” error in browser:

Uncaught ReferenceError: require is not defined
    js generated-stories-entry.js:3
    Webpack 7
        __webpack_require__
        __webpack_exec__
        <anonymous>
        O
        <anonymous>
        webpackJsonpCallback
        <anonymous>

To Reproduce

git clone git@github.com:arty-name/storybook-webpack5-module.git && yarn install && npx start-storybook

Alternatively these simple steps:

  1. Create package.json containing { "type": "module" }
  2. yarn add react react-dom
  3. yarn add --dev @storybook/react @storybook/builder-webpack5 @storybook/manager-webpack5
  4. Create .storybook/main.cjs containing module.exports = { core: { builder: 'webpack5' }, stories: ['../test.stories.js'] }
  5. Create test.stories.js containing:
export default { title: 'Story' }
export function Story() {
  return 'Pass!'
}
  1. npx start-storybook

System

Environment Info:

  System:
    OS: Linux 5.8 Ubuntu 21.04 (Hirsute Hippo)
    CPU: (8) x64 11th Gen Intel(R) Core(TM) i7-1165G7 @ 2.80GHz
  Binaries:
    Node: 16.3.0 - /usr/bin/node
    Yarn: 1.22.5 - /usr/bin/yarn
    npm: 7.15.1 - /usr/bin/npm
  Browsers:
    Chrome: 91.0.4472.114
    Firefox: 89.0.1
  npmPackages:
    @storybook/builder-webpack5: ^6.3.0 => 6.3.0 
    @storybook/manager-webpack5: ^6.3.0 => 6.3.0 
    @storybook/react: ^6.3.0 => 6.3.0 

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:33
  • Comments:43 (24 by maintainers)

github_iconTop GitHub Comments

25reactions
shilmancommented, Jan 5, 2022

If you want this fixed, please upvote by adding a 👍 to the issue description. We use this to help prioritize!

7reactions
arty-namecommented, Jul 19, 2021

The .storybook/package.json workaround works indeed, thanks!

Read more comments on GitHub >

github_iconTop Results From Across the Web

NodeJS (Server): ReferenceError: require is not defined when ...
As the documentation says: No require, exports, module.exports, __filename, __dirname These CommonJS variables are not available in ES ...
Read more >
Fix "require is not defined" in JavaScript/Node - Stack Abuse
You try to use require in Node when ES modules are specified in package.json via "type": "module"; You're trying to use require in...
Read more >
How To Fix ReferenceError require is not defined in JavaScript
You tried using require in a browser environment; You are in a Node.js environment but your project has "type": "module" in its package.json....
Read more >
How to fix the error `require is not defined` in Node.js
"type": "module",. It means that the default way of using npm modules is not require anymore, but import .
Read more >
ReferenceError: require is not defined #33741 - nodejs/node
In a "type: module" package should not even try calling any of commonly used cli devDependencies, like webpack-cli - they will all break...
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