web
web copied to clipboard
[dev-server-storybook] start fails due to require() of ES modules is not supported when package.json type: module is used
Hello.
There is an issue starting dev-server-storybook when it's used in a library with "type: module"
###Steps to reproduce:
- start a package with packcage.json
"type: module"(or for example clone or download https://github.com/lit/lit-element-starter-ts) - according to the current guide add the dev-server-storybook
- create a
.storybook/main.jswithexports.default =(N.B. - it's the source of issue) - create a
.storybook/server.config.mjs
- run
wds --config .storybook/server.config.mjs - got the error
ERR_REQUIRE_ESMrequire() of ES modules is not supported
> wds --config .storybook/server.config.mjs
Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: ~/lit-element-starter-ts-main/.storybook/main.js
require() of ES modules is not supported.
require() of ~/lit-element-starter-ts-main/.storybook/main.js from ~/lit-element-starter-ts-main/node_modules/@web/dev-server-storybook/dist/shared/config/readStorybookConfig.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 ~/lit-element-starter-ts-main/package.json.
Possible solution 1
remove "type: "module" from your package.json
Possible solution 2
- fix
packages/dev-server-storybook/src/shared/config/readStorybookConfig.tsrename hardcoded main.js to main.cjs - fix the docs and guide (rename hardcoded main.js to main.cjs)
This PR https://github.com/modernweb-dev/web/pull/1831 should fix the issue