已经给出

时间:2016-10-11 23:20:01

标签: reactjs webpack babeljs

我收到了意外的令牌错误,其中已经为react和es2015提供了预设的webpack.config.js错误./components/App.component.js中的ERROR我尝试了每个预设组合

Module parse failed: /home/krishna/AndroidStudioProjects/jancan/web/components/App.component.js Unexpected token (5:2)
You may need an appropriate loader to handle this file type.
SyntaxError: Unexpected token (5:2)
    at Parser.pp$4.raise (/home/krishna/AndroidStudioProjects/jancan/web/node_modules/webpack/node_modules/acorn/dist/acorn.js:2221:15)
    at Parser.pp.unexpected (/home/krishna/AndroidStudioProjects/jancan/web/node_modules/webpack/node_modules/acorn/dist/acorn.js:603:10)
    at Parser.pp$3.parseExprAtom (/home/krishna/AndroidStudioProjects/jancan/web/node_modules/webpack/node_modules/acorn/dist/acorn.js:1822:12)
    at Parser.pp$3.parseExprSubscripts (/home/krishna/AndroidStudioProjects/jancan/web/node_modules/webpack/node_modules/acorn/dist/acorn.js:1715:21)
    at Parser.pp$3.parseMaybeUnary (/home/krishna/AndroidStudioProjects/jancan/web/node_modules/webpack/node_modules/acorn/dist/acorn.js:1692:19)
    at Parser.pp$3.parseExprOps (/home/krishna/AndroidStudioProjects/jancan/web/node_modules/webpack/node_modules/acorn/dist/acorn.js:1637:21)
    at Parser.pp$3.parseMaybeConditional (/home/krishna/AndroidStudioProjects/jancan/web/node_modules/webpack/node_modules/acorn/dist/acorn.js:1620:21)
    at Parser.pp$3.parseMaybeAssign (/home/krishna/AndroidStudioProjects/jancan/web/node_modules/webpack/node_modules/acorn/dist/acorn.js:1597:21)
    at Parser.pp$3.parseParenAndDistinguishExpression (/home/krishna/AndroidStudioProjects/jancan/web/node_modules/webpack/node_modules/acorn/dist/acorn.js:1861:32)
    at Parser.pp$3.parseExprAtom (/home/krishna/AndroidStudioProjects/jancan/web/node_modules/webpack/node_modules/acorn/dist/acorn.js:1796:19)
    at Parser.pp$3.parseExprSubscripts (/home/krishna/AndroidStudioProjects/jancan/web/node_modules/webpack/node_modules/acorn/dist/acorn.js:1715:21)
    at Parser.pp$3.parseMaybeUnary (/home/krishna/AndroidStudioProjects/jancan/web/node_modules/webpack/node_modules/acorn/dist/acorn.js:1692:19)
    at Parser.pp$3.parseExprOps (/home/krishna/AndroidStudioProjects/jancan/web/node_modules/webpack/node_modules/acorn/dist/acorn.js:1637:21)
    at Parser.pp$3.parseMaybeConditional (/home/krishna/AndroidStudioProjects/jancan/web/node_modules/webpack/node_modules/acorn/dist/acorn.js:1620:21)
    at Parser.pp$3.parseMaybeAssign (/home/krishna/AndroidStudioProjects/jancan/web/node_modules/webpack/node_modules/acorn/dist/acorn.js:1597:21)
    at Parser.pp$3.parseFunctionBody (/home/krishna/AndroidStudioProjects/jancan/web/node_modules/webpack/node_modules/acorn/dist/acorn.js:2098:24)
    at Parser.pp$3.parseArrowExpression (/home/krishna/AndroidStudioProjects/jancan/web/node_modules/webpack/node_modules/acorn/dist/acorn.js:2087:10)
    at Parser.pp$3.parseParenArrowList (/home/krishna/AndroidStudioProjects/jancan/web/node_modules/webpack/node_modules/acorn/dist/acorn.js:1902:17)
    at Parser.pp$3.parseParenAndDistinguishExpression (/home/krishna/AndroidStudioProjects/jancan/web/node_modules/webpack/node_modules/acorn/dist/acorn.js:1870:21)
    at Parser.pp$3.parseExprAtom (/home/krishna/AndroidStudioProjects/jancan/web/node_modules/webpack/node_modules/acorn/dist/acorn.js:1796:19)
    at Parser.pp$3.parseExprSubscripts (/home/krishna/AndroidStudioProjects/jancan/web/node_modules/webpack/node_modules/acorn/dist/acorn.js:1715:21)
    at Parser.pp$3.parseMaybeUnary (/home/krishna/AndroidStudioProjects/jancan/web/node_modules/webpack/node_modules/acorn/dist/acorn.js:1692:19)
    at Parser.pp$3.parseExprOps (/home/krishna/AndroidStudioProjects/jancan/web/node_modules/webpack/node_modules/acorn/dist/acorn.js:1637:21)
    at Parser.pp$3.parseMaybeConditional (/home/krishna/AndroidStudioProjects/jancan/web/node_modules/webpack/node_modules/acorn/dist/acorn.js:1620:21)
    at Parser.pp$3.parseMaybeAssign (/home/krishna/AndroidStudioProjects/jancan/web/node_modules/webpack/node_modules/acorn/dist/acorn.js:1597:21)
    at Parser.pp$1.parseVar (/home/krishna/AndroidStudioProjects/jancan/web/node_modules/webpack/node_modules/acorn/dist/acorn.js:1034:28)
    at Parser.pp$1.parseVarStatement (/home/krishna/AndroidStudioProjects/jancan/web/node_modules/webpack/node_modules/acorn/dist/acorn.js:917:10)
    at Parser.pp$1.parseStatement (/home/krishna/AndroidStudioProjects/jancan/web/node_modules/webpack/node_modules/acorn/dist/acorn.js:706:19)
    at Parser.pp$1.parseTopLevel (/home/krishna/AndroidStudioProjects/jancan/web/node_modules/webpack/node_modules/acorn/dist/acorn.js:638:25)
    at Parser.parse (/home/krishna/AndroidStudioProjects/jancan/web/node_modules/webpack/node_modules/acorn/dist/acorn.js:516:17)
 @ multi main

这是App.component.js

    import React from 'react';
    import ReactDOM from 'react-dom';

    var App = () => (
        <h1>Hello World</h1>
    );

    ReactDOM.render(
        <App/>,
        document.getElementById('app')
    );

修改

webpack.config.js在评论中询问它不会转换jsx代码我使用babel-polyfill。我使用了babel es2015并对预设进行了反应,并将代码保存为.js

module.exports = {
    entry: ['babel-polyfill', './components/App.component.js'],
    output: {
        path: 'dist/',
        filename: "bundle.js"
    },
    loaders: [
    {
      test: /\.jsx?$/,
      exclude: /(node_modules|bower_components|dis)/,
      loader: 'babel', // 'babel-loader' is also a valid name to reference
      presets: ['react']
    }
  ]

};

1 个答案:

答案 0 :(得分:0)

我发现答案加载器应该在模块对象中