Webpack错误:模块解析失败

时间:2017-07-17 06:49:11

标签: reactjs webpack redux

我使用webpack相对较新。我的问题是,当我运行./node_modules/.bin/webpack -d时,我会遇到错误。我想知道任何人都可以帮助我并告诉我可能出错的地方。 错误如下:

`ERROR in ./src/components/App.js
 Module parse failed: /Users/danielmccord/webpack-
 demo/src/components/App.js Unexpected token (8:2)
 You may need an appropriate loader to handle this file type.
 | 
 | const App = () => (
 |   <div>
 |     <AddTodo />
 |     <VisibleTodoList />
 @ ./src/client/app/index.js 17:11-44`

Webpack配置

var webpack = require('webpack');
var path = require('path');
var BUILD_DIR = path.resolve(__dirname, 'public');
var APP_DIR = path.resolve(__dirname, 'src/client/app');
var config = {
    entry: APP_DIR + '/index.js',
    output: {
        path: BUILD_DIR,
        filename: 'bundle.js'
    },
    module: {
        loaders: [{
            test: /\.js?/,
            include: APP_DIR,
            loader: 'babel-loader'
        }]
    },
};
module.exports = config;

提前致谢

1 个答案:

答案 0 :(得分:0)

虽然您已为webpack配置指定了加载程序,但您尚未指定ES6预设且测试正则表达式不正确,请将配置更改为

module: {
    rules: [{
        test: /\.jsx?$/,
        include: APP_DIR,
        exclude: [/node_modules/],
        use: [{
            loader: "babel-loader",
            options: {
                        presets: ["stage-0","es2015","react"],
                        plugins: ["transform-class-properties"]
                    }
        }]  
    }]

},

P.S。确保使用

安装预设和插件
npm install -S babel-plugin-transform-class-properties babel-preset-es2015 babel-preset-react babel-preset-stage-0