Open cseas opened 3 years ago
Storybook uses babel to process typescript files. There have been requests to support ts-loader as well, but it's not on our roadmap.
When I remove type: "module"
from package.json
, storybook runs successfully. So the above error might be very much related to #11587 even though it's a different console error.
I figured out a workaround. Storybook won't work with type: "module"
in package.json
so that has to be removed. But es6 import/export syntax can still be used with the .mjs
extension so all I had to do was rename webpack.config.js
to webpack.config.mjs
and everything works.
On a side note, I recently learned that babel-loader doesn't perform any type checks. At work, we have observed that storybook often fails to report type errors that are later caught by the bundler when we start using the components developed individually with storybook. This might be another reason to consider using ts-loader for typescript instead.
@cseas Thanks for the heads up! We have an opt-in type checking option using fork-ts-checker-webpack-plugin
https://storybook.js.org/docs/react/configure/typescript#mainjs-configuration
In an nx monorepo I had "type": "commonjs"
in the corresponding package.json
file, removed the property and it worked.
I'm still seeing this issue with storybook 6.5 and a create-react-app
typescript project.
ModuleParseError: Module parse failed: 'import' and 'export' may appear only with 'sourceType: module' (3:0)
File was processed with these loaders:
* ./node_modules/@pmmmwh/react-refresh-webpack-plugin/loader/index.js
I tried the workaround from @shilman but am still getting the error. I've tried a lot of workarounds at this point and nothing quite gets around the problem.
Here's .storybook/main.cjs
:
module.exports = {
"stories": [
"../src/**/*.stories.mdx",
"../src/**/*.stories.@(js|jsx|ts|tsx)"
],
"addons": [
"@storybook/addon-links",
"@storybook/addon-essentials",
"@storybook/addon-interactions",
"@storybook/preset-create-react-app"
],
"framework": "@storybook/react",
"core": {
"builder": "@storybook/builder-webpack5"
},
"typescript": {
check: false,
checkOptions: {},
reactDocgen: 'react-docgen-typescript',
reactDocgenTypescriptOptions: {
shouldExtractLiteralValuesFromEnum: true,
propFilter: (prop) => (prop.parent ? !/node_modules/.test(prop.parent.fileName) : true),
},
},
}
and the storybook versions from package.json
:
"@storybook/addon-actions": "^6.5.16",
"@storybook/addon-essentials": "^6.5.16",
"@storybook/addon-interactions": "^6.5.16",
"@storybook/addon-links": "^6.5.16",
"@storybook/builder-webpack5": "^6.5.16",
"@storybook/manager-webpack5": "^6.5.16",
"@storybook/node-logger": "^6.5.16",
"@storybook/preset-create-react-app": "^4.1.2",
"@storybook/react": "^6.5.16",
"@storybook/testing-library": "^0.0.13",
Any help would be appreciated. I'm stuck with the CRA stuff for the time being unfortunately.
@cbroomemktw does upgrading to the latest prerelease solve it?
Migration guide: https://storybook.js.org/migration-guides/7.0
@shilman It did! At least installing everything from scratch via npx sb@next init
thanks!
Describe the bug Unable to run storybook after using
npx sb init --builder webpack5
To Reproduce
Above command doesn't work. I get the below error:
/bin/sh: 1: yarn: not found
Failed probably because I don't have yarn on my system (Ubuntu 20.04 LTS). But the repro's here: https://github.com/hazel-ui/hazel-ui/tree/feature/webpack Just runnpm i
followed bynpm run storybook
. I didn't change anything storybook related after running the abovesb init
command.System
Additional context I'm trying to do a fresh install of storybook in a React project that uses Webpack 5 and ts-loader. Also using
type: "module"
in package.json which led to an initial error I fixed by doing this.My best guess is that there's some problem with storybook wanting to use babel-loader but my project doesn't use babel at all, the whole transpilation is entirely handled by ts-loader as recommended in webpack docs. So as a preference, it'd be nice if Storybook can just use the webpack config instead of installing the additional babel packages.