storybook: Webpack5 + type: module = require is not defined
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:
- 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
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 39
- Comments: 54 (33 by maintainers)
Commits related to this issue
- demo storybookjs/storybook/issues/14877 — committed to arty-name/storybook-webpack5-module by arty-name 6 years ago
- fix ModuleParseError error message ``` ModuleParseError: Module parse failed: 'import' and 'export' may appear only with 'sourceType: module' ``` issues - https://github.com/storybookjs/storybook/is... — committed to ganezasan/hello-jest-cra-template by ganezasan 4 years ago
- Use webpack5 (#1) * use webpack5 and type: module * uninstall tsconfig-paths-webpack-plugin * fix webpackFinal: * fix ModuleParseError error message ``` ModuleParseError: Module parse f... — committed to ganezasan/hello-jest-cra-template by ganezasan 4 years ago
- build: fix storybook demo docs Try solution in https://github.com/storybookjs/storybook/issues/14877\#issuecomment-881981075. — committed to hyochan/dooboo-ui by hyochan 4 years ago
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!I looked into this a bit, and it’s a bit weird since the file as checked in to Git actually does use the
importsyntax. But it gets transpiled to CommonJS format in thecjsbuild of storybook, which I suppose is being used because we currently have to use.storybook/main.cjsinstead of just.storybook/main.js.Not quite sure what the proper solution is for this. One possibility is to maintain two copies of
virtualModuleStory.template.js, one for CJS and one for ESM, and then keeping them from being processed during the build, would work. Then the template code could usepackageJson.typeto determine which template to use. There might be a better solution though that lets webpack convert the template or something. Or probably best would be if Node would just run the ESM build of storybook.EDIT: Actually it’s more complicated than this since the template files also reference
moduleand are populated with code that usesrequire.context. In the end, I found it worked to create a.storybook/package.jsonfile with the contents just{}, nothing else. This seems to put webpack in a mode where it’s tolerant of both ES6 import/export and CommonJS require.I have a particularly horrible workaround based on https://github.com/storybookjs/storybook/issues/14877#issuecomment-973831859 . In the
main.*(c)jsfile you can change theVirtualModulesPlugininstance to add the.cjssuffix to the appropriate files. This is only likely to work for a specific version of Storybook so I’d suggest that you pin to a specific version if you’re going to use it.I’ve tested this with Storybook 6.4.9. It may work for other versions.
Still seeing this as an issue in 6.5.15 with
storyStoreV7true 😕 withbuild-storybook@jpzwarte I think the reason that the
.storybook/package.jsonsolution doesn’t work any more in 6.4 is because we “moved” the location of the virtual module created by the file referenced in @dimfeld’s comment to the root of the project, rather than the.storybookdirectory.Let me see if I understand this issue correctly:
{ "type": "module" }, webpack doesn’t want to seerequire()any morevirtualModuleStory.template.js, which was compiled to CJS) makes use ofrequire()./.storybook, if you put apackage.jsonin there declaring a non “module” package, WP is happy again.I wonder if there is an alternate way to tell WP that the bundle we are create is (to begin with at least) CJS, not ESM, so it doesn’t look in
./package.json:typeto figure that out?We would like to properly sort out this ESM/CJS business in 6.5 so it is possible to actually just use ESM everywhere.
I’ve updated the example repo from above and updated the packages to
6.4.0-beta.33and added the emptypackage.json, but I still get therequire is undefinederror. cc @shilmanHi! I ran into this issue and none of the workarounds worked for me so I tried
npx sb upgrade --prereleasebut I’m still seeing the same error whenever I try to add a webpack plugin inmain.jsYee-haw!! I just released https://github.com/storybookjs/storybook/releases/tag/v6.5.0-alpha.23 containing PR #16727 that references this issue. Upgrade today to the
@nextNPM tag to try it out!Closing this issue. Please re-open if you think there’s still more to do.
Would it make sense to remove the “has workaround” label meanwhile?
@shilman Our company’s project also happened after the execution of the storybook build page error: require is not defined, my colleagues spent a whole day to study the problem, and now the problem is solved, the difference is that we are using vite, but I searched for the problem when I found the issue, I think it should be a class of problems, the root cause of the problem is not defined, the problem is not defined. The root cause is that .storybook/main.js does not read the complete configuration of vite.config.js, I hope that someone later encountered can be solved in this way, or hope to read the complete vite related configuration.
The solution is to manually add the commonjs processing logic like in the image below, which should be a read omission issue?
I have solved problem by adding virtual package.json file in
.storybook/main.cjs:and add this plugin to the
webpackFinalproperty:It’s the workaround for solving problem with type: “module” in our package.json Storybook adds virtual module “storybook-config-entry.js” in the root of our project, then Webpack loads it as module and reads our package.json and think that storybook-config-entry.js is a es module and doesn’t make some transformation over code like
require. In result, stories fall with error “unknown require function”. Here we add a virtual package.json file which doesn’t contain prop “type”.Thanks to @jpzwarte for the fix here!
It works! Thank you very much for fixing it! 🙇
After a few hours of fiddling with it, I found an elegant solution. I’ve created a PR https://github.com/storybookjs/storybook/pull/17108 that replaces
requirewithimport.Currently, the extensions are not ensured, so you’ll probably still need to do some replacements. Here’s mine:
@supuwoerc Thank you!! This fixed the issue I was having with vite when pushing storybook to chromatic
Heres how I configured
.storybook/main.js:@tmeasday I created a PR last year in July to partially fix this issue, but it was never merged: https://github.com/storybookjs/storybook/pull/18620. I will do some quick testing in the afternoon to determine whether this is still a thing.
@tmeasday Thank you for checking! My projects seem to run fine with the SB7 prerelease, and I even could remove the workarounds. Well done!
Thanks @shilman!
Just don’t use the new on-demand architecture as it will break the workaround - the following configuration:
@philippone that’s intentional, and that’s how ESM support works in TypeScript. See: https://www.typescriptlang.org/docs/handbook/esm-node.html#type-in-packagejson-and-new-extensions (You can also run
npm run buildin the repo to show that it does work with TypeScript, it just doesn’t work with StoryBook.)If i remove the fully specified path (including the extension), then my library is not ESM compatible and Node will throw errors, so i can’t remove the extension.
@tmeasday I’ve tested it by changing
node_modules/@storybook/builder-webpack5/dist/cjs/preview/iframe-webpack.config.jsin place and the only thing that is required is to renamegenerated-stories-entry.jstogenerated-stories-entry.cjs.I can confirm that with that change, the
require is undefinederror is gone!