storybook: Module parse failed: You may need an appropriate loader to handle this file type
Sorry if this is an already answered issue but trust me when i told you that i spend most of the day googling about this and couldn’t find a solution.
I have an existing project (with custom webpack.config.dev.js file) that I want to integrate with storybooks but after following the getting started guide and installing whatever babel or any other kind of dependency available on the web, i’m still not able to overcome this annoing issue.
Module parse failed: ...\src\components\form\button\index.stories.js Unexpected token (10:2)
You may need an appropriate loader to handle this file type. | storiesOf('Button', module)
| .add('with text', () => (
| <Button onClick={action('clicked')}>Hello Button</Button>
| ))
| .add('with some emoji', () => (
at Object.<anonymous> (http://localhost:3001/static/preview.bundle.js:45665:7)
at __webpack_require__ (http://localhost:3001/static/preview.bundle.js:1203:30)
at fn (http://localhost:3001/static/preview.bundle.js:731:20)
at webpackContext (http://localhost:3001/static/preview.bundle.js:45646:9)
at req.keys.forEach (http://localhost:3001/static/preview.bundle.js:42662:35)
at Array.forEach (<anonymous>)
at loadStories (http://localhost:3001/static/preview.bundle.js:42662:13)
at ConfigApi._renderMain (http://localhost:3001/static/preview.bundle.js:43739:20)
at render (http://localhost:3001/static/preview.bundle.js:43765:17)
at ConfigApi.configure (http://localhost:3001/static/preview.bundle.js:43790:9)
Based on the error message i think it has something to do with the configuration in .babelrc. This is my actual setup
Package.json
"scripts": {
"storybook": "start-storybook -p 3001 -c ./config/.storybook",
"build-storybook": "build-storybook"
}
Config dir
Located in ./config/.storybook
./config/.storybook/
- .babelrc
- config.js
- webpack.config.dev.js
.babelrc
(I installed almost EVERYTHING I found related to babel)
{
"presets": ["es2015", "react", "stage-0"],
"plugins": [
"transform-runtime",
"transform-decorators-legacy",
"transform-class-properties",
"transform-object-rest-spread",
"react-hot-loader/babel",
"typecheck",
"transform-es2015-modules-commonjs",
"add-module-exports",
"import-asserts"
]
}
config.js
// Try to load stories from inside the `../../src/` dir where there are the components
const req = require.context('../../src', true, /(\.stories\.js$)|(\.stories\.jsx$)/);
function loadStories() {
req.keys().forEach((filename) => req(filename));
}
configure(loadStories, module);
webpack.config.dev.js
I’m trying to use the original webpack.config.dev.js file that I setup for my project development
var path = require('path');
var webpack = require('webpack');
var config = require('../webpack.config.dev.js');
module.exports = {
plugins: [
new webpack.NoErrorsPlugin(),
new webpack.HotModuleReplacementPlugin()
],
module: {
loaders: [
// Transform all ES6 files to plain old ES5.
{
test: /\.(js|jsx)$/,
exclude: [/bower_components/, /node_modules/, /styles/],
loader: 'babel',
include: path.resolve(__dirname, '../src')
},
// Fonts
{
test: /\.woff(2)?(\?v=[0-9]\.[0-9]\.[0-9])?$/,
loader: 'url-loader?limit=10000&mimetype=application/font-woff',
query: {
name: 'static/media/files/[name].[hash:8].[ext]'
}
}, {
test: /\.(ttf|eot|svg)(\?v=[0-9]\.[0-9]\.[0-9])?$/,
loader: 'file-loader',
query: {
name: 'static/media/fonts/[name].[hash:8].[ext]'
}
},
// Load images.
{
test: /\.(gif|jpe?g|png)$/,
loader: 'url-loader?limit=25000',
query: {
limit: 10000,
name: 'static/media/images/[name].[hash:8].[ext]'
}
},
// jSon Loader
{
test: /\.json$/,
loader: 'json-loader'
}, {
test: /\.scss$/,
loader: 'style!css!sass!resolve-url!sass?sourceMap&sourceComments'
}, {
test: /\.css$/,
loader: 'style!css?importLoaders=1'
}
]
}
};
Story file
And finally the story file
import React from 'react';
import { storiesOf } from '@storybook/react';
import { action } from '@storybook/addon-actions';
// Component
import { Button } from './index';
storiesOf('Button', module)
.add('with text', () => (
<Button onClick={action('clicked')}>Hello Button</Button>
))
.add('with some emoji', () => (
<Button onClick={action('clicked')}>😀 😎 👍 💯</Button>
));
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 19
- Comments: 68 (13 by maintainers)
for people facing this issue in newer versions of storybook:
.storybook/main.js
@igor-dv @Hypnosphi I tried:
git clean -fdxpackage-lock.jsonsince (1) does not delete itnpm installnpm run storybookNope. Still the same error.
@AssisrMatheus, @d-spiridonov, @lucasavila00 I created example repo with problem solution storybook-typescript-problem.
I’m not familiar with webpack at all, I try hard to avoid it 😄. It would be amazing if whatever the defaults are supported npm-linked stuff but I’ll try
I fixed this issue by looking at some of the
webpack.config.jsfiles from examples shared by storybooks. In the project that I’m working on they decided to move to another styleguide generator but I managed to make it work before moving it so in case someone want’s to have a look at it, here it’s my workingwebpack.configfile:I’m still running into this. @isc30 Your fix did not work for me 😢
[Solved] Failed to compile : ./node_modules/@react-leaflet/core/esm/path.js 10:41 Module parse failed: Unexpected token (10:41)
the easiest way to do that is
git clean -fdx(removes everything that’s not in git)@Luchanso Your example works! It saves my day, thank you.
@Kucerenko171 Do you use latest version of storybook?
@mmo0307 it looks working. Thank you
@Kucerenko171 Try to connect this loader { test: /.s[ac]ss$/i, use: [ // Creates style nodes from JS strings MiniCssExtractPlugin.loader, // Translates CSS into CommonJS “css-loader”, // Compiles Sass to CSS “sass-loader”, ], }
@isc30 thanks for this 👍 Can confirm this works for my case: Lerna create-react-app monorepo using Typescript. Now all my stories (other than the example introduction that comes shipped with Storybook) show up!
@Luchanso Your example works! It saves my year, thank you.
@Luchanso did you manage to get it to work?
@ndelangen example repository for reproduce problem:
https://github.com/Luchanso/storybook-typescript-problem
Outputs logs:
Maybe later I’ll try to play with webpack.config.js
@helloncanella @pouyajabbarisani I had set the include point to
src/index.jsinstead ofsrc/which fixed my issue.Common Path
Webpack.config.js
This is caused by having a JSX file in node_modules. Note that in the above webpack configs (and I’m having this same issue) the ones with the issues are excluding node_modules or only including the source directory for the project. One solution is ejecting with
npm run ejectand then adding the node_modules directory to the webpack configs.I’m unsure if there is a solution that doesn’t require ejecting – searching for one now.
private i will say…i came across a solution git submodule add repository
yes my solution was:
webpack.config.js
Sure ! There’s one on Storybook page, https://storybook.js.org/configurations/custom-webpack-config/ Let me know if you can fix your problem with this one. I adapted example above and it works like a charm on my configuration with scss.
+1, I’ve got the same issue