模块解析失败,使用 webpack 和 react,即使使用 babel-loader [英] Module parse failed with webpack and react, even using babel-loader

查看:25
本文介绍了模块解析失败,使用 webpack 和 react,即使使用 babel-loader的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 webpack 与 react 一起使用,但出现此错误:

I am trying to use webpack with react but am getting this error:

./app/main.js 中的错误

ERROR in ./app/main.js

Module parse failed: /Users/me/app/main.js Line 2: Unexpected token
You may need an appropriate loader to handle this file type.
| //npm
| import React               from 'react';

这是感兴趣的 webpack 配置部分:

This is the webpack config section of interest:

test: /\.jsx?$/,
        include: path.join(__dirname, 'src'),
        loader: 'babel-loader',
        query: {
            presets: ['es2015', 'react']

和 package.json:

and the package.json:

"devDependencies": {
    "autoprefixer-loader": "^3.1.0",
    "babel-core": "^6.1.20",
    "babel-loader": "^6.1.0",
    "babel-preset-es2015": "^6.1.18",
    "babel-preset-react": "^6.1.18",
    "jest": "^0.1.40",
    "webpack": "^1.12.4",
    "webpack-dev-server": "^1.12.1",
    "css-loader": "^0.22.0",
    "style-loader": "^0.13.0"
  },
  "dependencies": {
    "babel-polyfill": "^6.1.19",
    "history": "^1.17.0",
    "react": "^0.14.2",
    "react-dom": "^0.14.2",
    "react-mdl": "^1.0.4",
    "react-router": "^1.0.2"
  }

推荐答案

查看错误信息中的路径

模块解析失败:/Users/me/app/main.js Line 2: Unexpected token

Module parse failed: /Users/me/app/main.js Line 2: Unexpected token

该模块似乎不在 src 文件夹中.但是,您明确指定只有 path.join(__dirname, 'src') 中的模块才能通过 babel 加载器.

it seems that the module is not inside the src folder. However, you explicitly specified that only modules inside path.join(__dirname, 'src') should passed through the babel loader.

调整include,使其也包含main.js.

这篇关于模块解析失败,使用 webpack 和 react,即使使用 babel-loader的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆