Closed ocavue closed 2 years ago
Hi, @ocavue. I just merged #16184 that should prevent this in the future. For your case, if you follow the directions in that error message (rename main.js to end in .cjs
), it should clear up.
That circumvents the issue though because we still need to run main
in CommonJS. Is there a timeline when Storybook will work with pure ES packages?
Wen switching to cjs
SB throws a warning > Unable to find main.js:
So what's the solution to make it work? Still cannot import an ESM package into some storybooks here.
@binarykitchen I was able to get it working by creating an empty .storybook/package.json
: {}
. The only caveat is you have to make sure your svelte.config.js
is actually svelte.config.cjs
and uses require, not import. For me this was a trivial change.
I do suspect it's a bug in storybook that we can't just name the file main.cjs
.
Running Storybook v6.4.18 and renaming the extension to main.cjs
works even though the warning Unable to find main.js
is thrown. I could check that main.cjs
actually runs and the warning doesn't interfere in the functionality.
I was able to get it working by creating an empty
.storybook/package.json
:{}
.
@tyler-boyd Worked for me with SvelteKit 1.0.0-next.320, but apart from this change i had to replace require('../svelte.config.js')
with import('../svelte.config.js')
in main.js
and install babel-plugin-transform-vite-meta-env
whose absence came up in an error when running npm run storybook
.
Didn't need to rename svelte.config.js
though.
Why this issue closed?.. Version 6.4.22 still has bad development experience in esm + typescript packages. 🙁
Still not working for me neither, I have the builder-vite, my confing in TypeScript and my project in ESM and I cannot start storybook.
(node:10329) UnhandledPromiseRejectionWarning: Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: node_modules/@babel/runtime/helpers/esm/regeneratorRuntime.js
require() of ES modules is not supported.
require() of node_modules/@babel/runtime/helpers/esm/regeneratorRuntime.js from .storybook/main.ts is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.
Instead rename regeneratorRuntime.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from node_modules/@babel/runtime/helpers/esm/package.json.
Changing file extension .storybook/main.js
to .storybook/main.cjs
and replacing any require()
with import()
in main.cjs
(as per error instructions) allowed storybook to launch successfully in Typescript ESM project.
Then you lose the benefit of ts. We try to avoid any commonjs file in our project.
But today it is working by using main.ts
with imports just at then end you have to use module.exports = config
otherwise you get the babel helpers injected and this is where you are getting the complication.
I guess the best practice would be to avoid default exports, that is the "only" real issue.
I would still prefer a complete support of ESM by storybook to avoid these tricky workaround that no one knows the exact working solution.
Same issue for me, a lot of errors with storybook 6.5.7 NodeV16 Main.ts file with vue3 project
I need to change 2 files to fix:
./storybook/main.ts
by removing export default
and use module.export
postcss.config.ts
by removing export default
and use module.export
too !
BUT now I am not able to import anything inside the postcss.config.ts
Describe the bug
I can't run the storybook within an ESM package (
"type": "module"
inpackage.json
).To Reproduce
https://github.com/issueset/storybook-esm-issue
System
Additional context
Since Storybook will remove CJS in 7.0 itself, then I expect it has a better support to an ESM-only project. Or at least has better document about how to use storybook in an ESM-only project.