【问题标题】:Webpack Error: Module parse failedWebpack 错误:模块解析失败
【发布时间】:2017-07-17 06:49:10
【问题描述】:

我对使用 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;

提前致谢

【问题讨论】:

  • 能否请您添加您的 webpack 配置代码
  • 你使用的是哪个版本的 webpack
  • 我使用的是版本 3.3.0

标签: reactjs webpack redux


【解决方案1】:

虽然你已经为你的 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"]
                    }
        }]  
    }]

},

附:确保使用

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

【讨论】:

  • 我将配置更改为上面的内容,但我现在收到一个新错误,提示“错误:'output.filename' is required, 无论是在配置文件中还是作为 --output-filename” ?但是我已经有一个 bundle.js 文件了?困惑!
  • 是的,谢谢。删除了错误,但得到了一个新错误,说“./src/client/app/index.js 中的错误模块构建失败:ReferenceError:未知插件“transform-class-properties " 在 "base" 中指定为 0,尝试相对于 "/Users/danielmccord/webpack-demo/src/client/app..." 进行解析
  • 感谢您的帮助,但是一旦我做了所有这些,它就让我回到了最初的错误。有什么建议吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-11-20
  • 2021-11-26
  • 1970-01-01
  • 2020-06-24
  • 2017-06-01
  • 2019-10-11
  • 2023-03-14
相关资源
最近更新 更多