Webpack5 + type: module = require is not defined
See original GitHub issueDescribe 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:
- Create
package.jsoncontaining{ "type": "module" } yarn add react react-domyarn add --dev @storybook/react @storybook/builder-webpack5 @storybook/manager-webpack5- Create
.storybook/main.cjscontainingmodule.exports = { core: { builder: 'webpack5' }, stories: ['../test.stories.js'] } - Create
test.stories.jscontaining:
export default { title: 'Story' }
export function Story() {
return 'Pass!'
}
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:
- Created 2 years ago
- Reactions:33
- Comments:43 (24 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

If you want this fixed, please upvote by adding a 👍 to the issue description. We use this to help prioritize!
The
.storybook/package.jsonworkaround works indeed, thanks!