Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Storybook cannot be built on packages using "type": "module" #11587

Closed
vvanpo opened this issue Jul 16, 2020 · 45 comments
Closed

Storybook cannot be built on packages using "type": "module" #11587

vvanpo opened this issue Jul 16, 2020 · 45 comments

Comments

@vvanpo
Copy link
Contributor

vvanpo commented Jul 16, 2020

Describe the bug

For projects configured to use native ES modules in node.js via the "type": "module" configuration in package.json (https://nodejs.org/api/esm.html#esm_package_json_type_field), Storybook cannot require() the main.js configuration file, because node does not support require() for this package type. We also cannot rename it to main.cjs to allow the use of require(), because Storybook does not recognize that file.

yarn run v1.22.4                                                                         
$ build-storybook -c .storybook -o storybook-out  -s ./static                            
info @storybook/vue v5.3.19                                                              
info                                                                                     
info clean outputDir..                                                                   
info => Copying static files from: ./static                                              
info => Copying prebuild dll's..                                                         
info => Building manager..                                                               
info => Loading manager config..                                                         
(node:38218) UnhandledPromiseRejectionWarning: Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /.../.storybook/main.js
require() of ES modules is not supported.                                                
require() of /.../.storybook/main.js from /.../node_modules/@storybook/core/dist/server/utils/server-require.
js 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 main.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from /.../package.json.
                                                                                         
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1154:13)            
    at Module.load (internal/modules/cjs/loader.js:986:32)
    at Function.Module._load (internal/modules/cjs/loader.js:879:14)
    at Module.require (internal/modules/cjs/loader.js:1026:19)
    at require (internal/modules/cjs/helpers.js:72:18)
    at interopRequireDefault (/.../node_modules/@storybook/core/dist/server/utils/server-require.js:63:18)

To Reproduce
In any project using Storybook, add "type": "module" to your package.json and observe broken builds (even after converting all your commonjs to esm).

Expected behavior
Storybook should provide some mechanism for allowing configuration in packages that use the module type.

System:

    OS: macOS 10.15.5
    CPU: (8) x64 Intel(R) Core(TM) i7-4870HQ CPU @ 2.50GHz
  Binaries:
    Node: 12.18.2 - /usr/local/bin/node
    Yarn: 1.22.4 - /usr/local/bin/yarn
    npm: 6.14.5 - /usr/local/bin/npm
  Browsers:
    Chrome: 83.0.4103.116
    Firefox: 73.0
    Safari: 13.1.1
  npmPackages:
    @storybook/addon-a11y: ^5.3.19 => 5.3.19 
    @storybook/addon-knobs: ^5.3.17 => 5.3.19 
    @storybook/addons: ^5.3.17 => 5.3.19 
    @storybook/vue: ^5.3.17 => 5.3.19
@vvanpo
Copy link
Contributor Author

vvanpo commented Jul 16, 2020

A cheap fix would be to just look for main.cjs in addition to main.js. Better might be to register @babel/register within server-require.js and use import().

@vvanpo vvanpo changed the title Storybook cannot be built on package's using "type": "module" Storybook cannot be built on packages using "type": "module" Jul 16, 2020
@shilman
Copy link
Member

shilman commented Jul 16, 2020

@ndelangen @igor-dv can you give a hand here?

@vvanpo
Copy link
Contributor Author

vvanpo commented Jul 17, 2020

Upon taking a closer look, it appears .cjs+.mjs support was added recently: 6b25bf5

So I'll just have to wait until v6 is released. Still, it would be nice if we could figure out a solution that doesn't require renaming the file.

@stale
Copy link

stale bot commented Aug 8, 2020

Hi everyone! Seems like there hasn't been much going on in this issue lately. If there are still questions, comments, or bugs, please feel free to continue the discussion. Unfortunately, we don't have time to get to every issue. We are always open to contributions so please send us a pull request if you would like to help. Inactive issues will be closed after 30 days. Thanks!

@stale stale bot added the inactive label Aug 8, 2020
@ndelangen
Copy link
Member

Assuming the above is true, I'll close this now, if it's still an issue, we can re-open!

@MrX8503
Copy link

MrX8503 commented Oct 2, 2020

I'm not able to get this working with CRA.

  • Node v12.18.4
  • @storybook/react v6.0.22
  • "type": "module" in package.json
  • renamed main.js to main.cjs

I get the warning "Unable to find main.js"

@shilman shilman reopened this Oct 2, 2020
@stale stale bot removed the inactive label Oct 2, 2020
@shilman
Copy link
Member

shilman commented Oct 2, 2020

reopening cc @yannbf

@rue-mishakorablin
Copy link

The interesting aspect of this failure is that the exit code is still 0, so this doesn't get caught as a failure in some CI environments like GitHub Actions. I think this is because the error happens in a promise, so this triggers UnhandledPromiseRejectionWarning rather than an exception.

@suared
Copy link

suared commented Dec 2, 2020

+1 on this. What I heard about Storybook sounded interesting but trying to go through the quick start with a super basic example fails. Used the demo from lit-eleent to start with: https://github.com/Polymer/lit-element to get a basic structure in place. Then started following the install instructions from storybook. Fails with the subject error here. moving from .js to .cjs does not solve the issue. Added as watcher to investigate storybook when ready to work with modules.

@shilman
Copy link
Member

shilman commented Dec 3, 2020

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

@stale
Copy link

stale bot commented Dec 25, 2020

Hi everyone! Seems like there hasn't been much going on in this issue lately. If there are still questions, comments, or bugs, please feel free to continue the discussion. Unfortunately, we don't have time to get to every issue. We are always open to contributions so please send us a pull request if you would like to help. Inactive issues will be closed after 30 days. Thanks!

@stale stale bot added the inactive label Dec 25, 2020
@kohloth
Copy link

kohloth commented Feb 6, 2021

Am experiencing this too. Just got a basic SvelteJS set up and fancied running storybook along with it.

Error in the terminal is:

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /computer/projectPath/etc/.storybook/main.js
require() of ES modules is not supported.

I installed storybook as per the npm package page (https://www.npmjs.com/package/@storybook/svelte) using npx -p @storybook/cli sb init.

Node version is 14.15.3

The Storybook install command put this in my package.json

    "@storybook/addon-actions": "^6.1.17",
    "@storybook/addon-essentials": "^6.1.17",
    "@storybook/addon-links": "^6.1.17",
    "@storybook/svelte": "^6.1.17",

Tried taking "type": "module", out of the package.json, but then the app itself stops working. Not really sure what Storybook wants.

@stale stale bot removed the inactive label Feb 6, 2021
@jeremytenjo
Copy link

Issue persists:

  • Node v14.15.4
  • @storybook/react v6.1.18
  • "type": "module" in package.json
  • renamed main.js to main.cjs

I get the warning "Unable to find main.js"

@wpatter6
Copy link

wpatter6 commented Mar 18, 2021

I tried the workaround mentioned in this issue of adding an empty object package.json into the .storybook folder for it to be treated as a commonjs type, and now I get stuck building storybook, always at this acorn-jsx/index.js:
Screen Shot 2021-03-18 at 1 06 55 PM

Renaming main.js to main.cjs gives the "unable to find main.js" warning but also gets stuck at the same place.

Removing "type": "module" from the root package.json and not including any of the above changes, storybook builds and runs correctly.

@jeanfredrik
Copy link

I solved it by adding a package.json file inside the .storybook folder. The file could have any content really, but an empty object gives an error, so I went with this:

{
  "type": "commonjs"
}

@unknownterritory
Copy link

I don't think the workaround is working anymore.
I'm trying to integrate Storybook in a clean installation of SvelteKit followed by either npx sb init or npx sb@next init.
Tried adding the package.json file into the .storybook folder.
Also tried to change .storybook/main.js and .storybook/preview.js to .cjs extensions.
I would suggest removing the has workaround label.

@shilman
Copy link
Member

shilman commented May 10, 2021

@yannbf is this one that you've looked at before in another issue?

@unknownterritory
Copy link

On the particular configuration I'm working on — SvelteKit + Storybook — it was possible to get Storybook to run by changing the extensions of .storybook/main.js and .storybook/preview.js to .cjs and removing the mention to svelte.config.js in .storybook/main.cjs.

Credit to @muj on the #svelte channel of the Storybook discord.

@IanVS
Copy link
Member

IanVS commented Apr 14, 2022

@bartenra @JU12000 as far as I know, the main.js file cannot actually be an ESM file, the rest of storybook assumes that the code which runs in node is commonjs. So, can you try renaming your main.js file to main.cjs?

@JU12000
Copy link

JU12000 commented Apr 14, 2022

@IanVS Renaming main.js to main.cjs works but only if you also rename svelte.config.js to svelte.config.cjs. To me this seems like a more cumbersome change because it's editing files outside of the immediate scope of storybook. IMO it would be better to have this working out of the box without the need to enforce that users give their svelte.config a certain file ending as it's impossible to know what else that might conflict with.

EDIT: I just realized that another way to change main.js to main.cjs without needing to rename svelte.config is to change the name of the file as well as changing the require statement to the import statement from my previous comment. That removes the need for the extra package.json in the .storybook folder as well.

JU12000 added a commit to JU12000/SvelteKitTest that referenced this issue Apr 14, 2022
karrui added a commit to opengovsg/design-system that referenced this issue Apr 25, 2022
)

* build: move bundler to vite

* chore: remove unused rollup deps after moving to vite

* refactor: use treeshakeable lodash import

* build(vite): add esm and cjs outputs

* feat: update target to es6 for better compat

* fix(storybook): add empty package.json so it can run with main.js

since root package.json is now type: module, .js files are now treated as ESM files... which storybook does not support.
See storybookjs/storybook#11587 (comment)
danielgamage added a commit to oakstudios/mechanical-ragger that referenced this issue May 16, 2022
@gangal86
Copy link

I solved it by adding a package.json file inside the .storybook folder. The file could have any content really, but an empty object gives an error, so I went with this:

{
  "type": "commonjs"
}

it works for me, thanks

@yannbf
Copy link
Member

yannbf commented Jul 22, 2022

FYI this issue was not really solved in v6.5.0-alpha.42, but it was fixed in v7.0.0-alpha.11 from this PR: #18619

This means that in Storybook 7 you can use ESM for your main.js files out of the box.

@CasperSocio
Copy link

CasperSocio commented Sep 12, 2022

Solution Summary

I was struggling with this issue for a while and there's a ton of information here, so I'll just summarise what worked for a Storybook 6 Svelte Vite setup in September 2022.

Step 1

Rename .storybook/main.js to .storybook/main.cjs

Step 2

In .storybook/main.cjs replace require with import:

module.exports = {
  // ...
  svelteOptions: {
    preprocess: import('../svelte.config.js').preprocess
  },
  // ...
}

Remember to include the file extension .js.

@NexZhu
Copy link

NexZhu commented Nov 10, 2022

@shilman @yannbf Hi, I'm trying @storybook/vue3-vite@7.0.0-alpha.47
I have "type": "module" in my package.json, .storybook/main.ts and .storybook/preview.ts using ESM import, however storybook dev still fails and the error log shows that the command is still running the CommonJS version of Storybook:

ERR! TypeError [ERR_INVALID_ARG_VALUE]: The argument 'filename' must be a file URL object, file URL string, or absolute path string. Received undefined
ERR!     at __node_internal_captureLargerStackTrace (node:internal/errors:484:5)
ERR!     at new NodeError (node:internal/errors:393:5)
ERR!     at createRequire (node:internal/modules/cjs/loader:1278:11)
ERR!     at Function.MarkdownItShiki (/root/dev/web/vue-component-library-starter/node_modules/.pnpm/markdown-it-shiki@0.6.1/node_modules/markdown-it-shiki/dist/index.js:69:40)
ERR!     at MarkdownIt.MarkdownIt.use (/root/dev/web/vue-component-library-starter/node_modules/.pnpm/markdown-it@13.0.1/node_modules/markdown-it/lib/index.js:497:10)
ERR!     at Object.markdownItSetup (/root/dev/web/vue-component-library-starter/vite.common.ts:62:10)
ERR!     at createMarkdown (/root/dev/web/vue-component-library-starter/node_modules/.pnpm/vite-plugin-vue-markdown@0.22.1_vite@3.2.2/node_modules/vite-plugin-vue-markdown/dist/index.js:85:11)
ERR!     at VitePluginMarkdown (/root/dev/web/vue-component-library-starter/node_modules/.pnpm/vite-plugin-vue-markdown@0.22.1_vite@3.2.2/node_modules/vite-plugin-vue-markdown/dist/index.js:272:25)
ERR!     at Object.<anonymous> (/root/dev/web/vue-component-library-starter/vite.common.ts:57:3)
ERR!     at Module._compile (node:internal/modules/cjs/loader:1159:14)
ERR!     at Module._compile (/root/dev/web/vue-component-library-starter/node_modules/.pnpm/esbuild-register@3.3.3_esbuild@0.14.54/node_modules/esbuild-register/dist/node.js:2258:26)
ERR!     at Module._extensions..js (node:internal/modules/cjs/loader:1213:10)
ERR!     at Object.newLoader [as .ts] (/root/dev/web/vue-component-library-starter/node_modules/.pnpm/esbuild-register@3.3.3_esbuild@0.14.54/node_modules/esbuild-register/dist/node.js:2262:9)
ERR!     at Module.load (node:internal/modules/cjs/loader:1037:32)
ERR!     at Module._load (node:internal/modules/cjs/loader:878:12)
ERR!     at Module.require (node:internal/modules/cjs/loader:1061:19)
ERR!     at require (node:internal/modules/cjs/helpers:103:18)
ERR!     at Object.<anonymous> (/root/dev/web/vue-component-library-starter/.storybook/main.ts:3:45)
ERR!     at Module._compile (node:internal/modules/cjs/loader:1159:14)
ERR!     at Module._compile (/root/dev/web/vue-component-library-starter/node_modules/.pnpm/esbuild-register@3.3.3_esbuild@0.14.54/node_modules/esbuild-register/dist/node.js:2258:26)
ERR!     at Module._extensions..js (node:internal/modules/cjs/loader:1213:10)
ERR!     at Object.newLoader [as .ts] (/root/dev/web/vue-component-library-starter/node_modules/.pnpm/esbuild-register@3.3.3_esbuild@0.14.54/node_modules/esbuild-register/dist/node.js:2262:9)
ERR!     at Module.load (node:internal/modules/cjs/loader:1037:32)
ERR!     at Module._load (node:internal/modules/cjs/loader:878:12)
ERR!     at Module.require (node:internal/modules/cjs/loader:1061:19)
ERR!     at require (node:internal/modules/cjs/helpers:103:18)
ERR!     at R (/root/dev/web/vue-component-library-starter/node_modules/.pnpm/@storybook+core-common@7.0.0-alpha.47_yalvw3r2waubxycyb7k7qsruca/node_modules/@storybook/core-common/dist/index.js:6:16)
ERR!     at b (/root/dev/web/vue-component-library-starter/node_modules/.pnpm/@storybook+core-common@7.0.0-alpha.47_yalvw3r2waubxycyb7k7qsruca/node_modules/@storybook/core-common/dist/index.js:6:204)
ERR!     at $r (/root/dev/web/vue-component-library-starter/node_modules/.pnpm/@storybook+core-common@7.0.0-alpha.47_yalvw3r2waubxycyb7k7qsruca/node_modules/@storybook/core-common/dist/index.js:16:3928)
ERR!     at buildDevStandalone (/root/dev/web/vue-component-library-starter/node_modules/.pnpm/@storybook+core-server@7.0.0-alpha.47_yalvw3r2waubxycyb7k7qsruca/node_modules/@storybook/core-server/dist/cjs/build-dev.js:92:56)
ERR!     at async withTelemetry (/root/dev/web/vue-component-library-starter/node_modules/.pnpm/@storybook+core-server@7.0.0-alpha.47_yalvw3r2waubxycyb7k7qsruca/node_modules/@storybook/core-server/dist/cjs/withTelemetry.js:72:5)
ERR!     at async In (/root/dev/web/vue-component-library-starter/node_modules/.pnpm/@storybook+cli@7.0.0-alpha.47_yalvw3r2waubxycyb7k7qsruca/node_modules/@storybook/cli/dist/generate.js:277:2113)
ERR!  TypeError [ERR_INVALID_ARG_VALUE]: The argument 'filename' must be a file URL object, file URL string, or absolute path string. Received undefined
ERR!     at __node_internal_captureLargerStackTrace (node:internal/errors:484:5)
ERR!     at new NodeError (node:internal/errors:393:5)
ERR!     at createRequire (node:internal/modules/cjs/loader:1278:11)
ERR!     at Function.MarkdownItShiki (/root/dev/web/vue-component-library-starter/node_modules/.pnpm/markdown-it-shiki@0.6.1/node_modules/markdown-it-shiki/dist/index.js:69:40)
ERR!     at MarkdownIt.MarkdownIt.use (/root/dev/web/vue-component-library-starter/node_modules/.pnpm/markdown-it@13.0.1/node_modules/markdown-it/lib/index.js:497:10)
ERR!     at Object.markdownItSetup (/root/dev/web/vue-component-library-starter/vite.common.ts:62:10)
ERR!     at createMarkdown (/root/dev/web/vue-component-library-starter/node_modules/.pnpm/vite-plugin-vue-markdown@0.22.1_vite@3.2.2/node_modules/vite-plugin-vue-markdown/dist/index.js:85:11)
ERR!     at VitePluginMarkdown (/root/dev/web/vue-component-library-starter/node_modules/.pnpm/vite-plugin-vue-markdown@0.22.1_vite@3.2.2/node_modules/vite-plugin-vue-markdown/dist/index.js:272:25)
ERR!     at Object.<anonymous> (/root/dev/web/vue-component-library-starter/vite.common.ts:57:3)
ERR!     at Module._compile (node:internal/modules/cjs/loader:1159:14)
ERR!     at Module._compile (/root/dev/web/vue-component-library-starter/node_modules/.pnpm/esbuild-register@3.3.3_esbuild@0.14.54/node_modules/esbuild-register/dist/node.js:2258:26)
ERR!     at Module._extensions..js (node:internal/modules/cjs/loader:1213:10)
ERR!     at Object.newLoader [as .ts] (/root/dev/web/vue-component-library-starter/node_modules/.pnpm/esbuild-register@3.3.3_esbuild@0.14.54/node_modules/esbuild-register/dist/node.js:2262:9)
ERR!     at Module.load (node:internal/modules/cjs/loader:1037:32)
ERR!     at Module._load (node:internal/modules/cjs/loader:878:12)
ERR!     at Module.require (node:internal/modules/cjs/loader:1061:19)
ERR!     at require (node:internal/modules/cjs/helpers:103:18)
ERR!     at Object.<anonymous> (/root/dev/web/vue-component-library-starter/.storybook/main.ts:3:45)
ERR!     at Module._compile (node:internal/modules/cjs/loader:1159:14)
ERR!     at Module._compile (/root/dev/web/vue-component-library-starter/node_modules/.pnpm/esbuild-register@3.3.3_esbuild@0.14.54/node_modules/esbuild-register/dist/node.js:2258:26)
ERR!     at Module._extensions..js (node:internal/modules/cjs/loader:1213:10)
ERR!     at Object.newLoader [as .ts] (/root/dev/web/vue-component-library-starter/node_modules/.pnpm/esbuild-register@3.3.3_esbuild@0.14.54/node_modules/esbuild-register/dist/node.js:2262:9)
ERR!     at Module.load (node:internal/modules/cjs/loader:1037:32)
ERR!     at Module._load (node:internal/modules/cjs/loader:878:12)
ERR!     at Module.require (node:internal/modules/cjs/loader:1061:19)
ERR!     at require (node:internal/modules/cjs/helpers:103:18)
ERR!     at R (/root/dev/web/vue-component-library-starter/node_modules/.pnpm/@storybook+core-common@7.0.0-alpha.47_yalvw3r2waubxycyb7k7qsruca/node_modules/@storybook/core-common/dist/index.js:6:16)
ERR!     at b (/root/dev/web/vue-component-library-starter/node_modules/.pnpm/@storybook+core-common@7.0.0-alpha.47_yalvw3r2waubxycyb7k7qsruca/node_modules/@storybook/core-common/dist/index.js:6:204)
ERR!     at $r (/root/dev/web/vue-component-library-starter/node_modules/.pnpm/@storybook+core-common@7.0.0-alpha.47_yalvw3r2waubxycyb7k7qsruca/node_modules/@storybook/core-common/dist/index.js:16:3928)
ERR!     at buildDevStandalone (/root/dev/web/vue-component-library-starter/node_modules/.pnpm/@storybook+core-server@7.0.0-alpha.47_yalvw3r2waubxycyb7k7qsruca/node_modules/@storybook/core-server/dist/cjs/build-dev.js:92:56)
ERR!     at async withTelemetry (/root/dev/web/vue-component-library-starter/node_modules/.pnpm/@storybook+core-server@7.0.0-alpha.47_yalvw3r2waubxycyb7k7qsruca/node_modules/@storybook/core-server/dist/cjs/withTelemetry.js:72:5)
ERR!     at async In (/root/dev/web/vue-component-library-starter/node_modules/.pnpm/@storybook+cli@7.0.0-alpha.47_yalvw3r2waubxycyb7k7qsruca/node_modules/@storybook/cli/dist/generate.js:277:2113) {
ERR!   code: 'ERR_INVALID_ARG_VALUE'
ERR! }

As you can see in the last few lines it's still running the CommonJS version:
node_modules/.pnpm/@storybook+core-server@7.0.0-alpha.47_yalvw3r2waubxycyb7k7qsruca/node_modules/@storybook/core-server/dist/cjs/withTelemetry.js:72:5

What might be wrong? Thanks!

Reproduction: https://github.com/daotl/vue-component-library-starter/tree/feat/storybook7

@IanVS
Copy link
Member

IanVS commented Nov 10, 2022

@NexZhu what moduleResolution are you using in your TS config?

@NexZhu
Copy link

NexZhu commented Nov 10, 2022

@IanVS It was node, I've just tried classic, nodenext, the same result.

dhess added a commit to hackworthltd/tidy that referenced this issue Nov 26, 2022
Specifically:

* Remove UMD module support. Per caniuse.com, all browsers that
support WebAssembly also support ES6, save Firefox < 54, which is now
over 5 years old.

* Add `files` to the package config and ensure that all files in
`dist` and `wasm_dist` are included in the package.

Note that Storybook expects CommonJS format, and adding `type: module`
to `package.json` causes it to break. This will be fixed in Storybook
v7, but until then, we implement the workaround described here:

storybookjs/storybook#11587 (comment)
dhess added a commit to hackworthltd/tidy that referenced this issue Nov 27, 2022
Specifically:

* Remove UMD module support. Per caniuse.com, all browsers that
support WebAssembly also support ES6, save Firefox < 54, which is now
over 5 years old.

* Add `files` to the package config and ensure that all files in
`dist` and `wasm_dist` are included in the package.

Note that Storybook expects CommonJS format, and adding `type: module`
to `package.json` causes it to break. This will be fixed in Storybook
v7, but until then, we implement the workaround described here:

storybookjs/storybook#11587 (comment)
dhess added a commit to hackworthltd/tidy that referenced this issue Nov 27, 2022
Specifically:

* Remove UMD module support. Per caniuse.com, all browsers that
support WebAssembly also support ES6, save Firefox < 54, which is now
over 5 years old.

* Add `files` to the package config and ensure that all files in
`dist` and `wasm_dist` are included in the package.

Note that Storybook expects CommonJS format, and adding `type: module`
to `package.json` causes it to break. This will be fixed in Storybook
v7, but until then, we implement the workaround described here:

storybookjs/storybook#11587 (comment)
dhess added a commit to hackworthltd/tidy that referenced this issue Nov 27, 2022
Specifically:

* Remove UMD module support. Per caniuse.com, all browsers that
support WebAssembly also support ES6, save Firefox < 54, which is now
over 5 years old.

* Add `files` to the package config and ensure that all files in
`dist` and `wasm_dist` are included in the package.

Note that Storybook expects CommonJS format, and adding `type: module`
to `package.json` causes it to break. This will be fixed in Storybook
v7, but until then, we implement the workaround described here:

storybookjs/storybook#11587 (comment)
@cseas
Copy link

cseas commented Jan 8, 2023

ESM-only Solution

I just got storybook running in a react project with "type": "module" in package.json and "module": "NodeNext" in tsconfig.json. No cjs workarounds required. Here are the changes I made after upgrading to storybook 7.0.0-beta.20.

Override the webpack config of Storybook like this:

// .storybook/main.js
export default {
  // ... 
  webpackFinal: async (config) => {
    const customConfig = { ...config };
    // use ts-loader to process typescript files
    customConfig.module.rules.push({
      test: /\.tsx?$/,
      use: "ts-loader",
      exclude: /node_modules/,
    });
    // Configure webpack to allow using .js extension for typescript file imports.
    // You only need this if you're adding .js to relative imports in your project.
    // Refer: https://github.com/microsoft/TypeScript/issues/42813#issuecomment-942633095
    customConfig.resolve.extensionAlias = {
      ".js": [".tsx", ".ts", ".js"],
    };
    return customConfig;
  },
};

dhess added a commit to hackworthltd/tidy that referenced this issue Jan 29, 2023
Specifically:

* Remove UMD module support. Per caniuse.com, all browsers that
support WebAssembly also support ES6, save Firefox < 54, which is now
over 5 years old.

* Add `files` to the package config and ensure that all files in
`dist` and `wasm_dist` are included in the package.

Note that Storybook expects CommonJS format, and adding `type: module`
to `package.json` causes it to break. This will be fixed in Storybook
v7, but until then, we implement the workaround described here:

storybookjs/storybook#11587 (comment)
georgefst pushed a commit to hackworthltd/tidy that referenced this issue Mar 23, 2023
Specifically:

* Remove UMD module support. Per caniuse.com, all browsers that
support WebAssembly also support ES6, save Firefox < 54, which is now
over 5 years old.

* Add `files` to the package config and ensure that all files in
`dist` and `wasm_dist` are included in the package.

Note that Storybook expects CommonJS format, and adding `type: module`
to `package.json` causes it to break. This will be fixed in Storybook
v7, but until then, we implement the workaround described here:

storybookjs/storybook#11587 (comment)
@laverdet
Copy link

I'm new to Storybook and was stuck on this for a while. The virtual storybook-config-entry.js file uses import, import.meta, and require all in the same file. Wow!

I worked around it with this horrific rule in my Webpack loader chain.

{
	test: /storybook-config-entry/,
	type: "javascript/dynamic",
	rules: [ { 
		loader: "babel-loader",
		options: {
			babelrc: false,
			configFile: false,
			plugins: [ "@babel/plugin-transform-modules-commonjs" ],
		}
	}, {
		loader: "string-replace-loader",
		options: {
			search: "import.meta.webpackHot",
			replace: "module.webpackHot",
			flags: "g",
		},
	} ],
}

Install loaders & plugins from npm:
https://www.npmjs.com/package/@babel/plugin-transform-modules-commonjs
https://www.npmjs.com/package/string-replace-loader

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests