loader jsx SyntaxError: Unexpected token

This question already has an answer here:

  • Babel file is copied without being transformed 6 answers

  • Add "babel-preset-react"

    npm install babel-preset-react
    

    and add "presets" option to babel-loader in your webpack.config.js

    (or you can add it to your .babelrc or package.js: http://babeljs.io/docs/usage/babelrc/)

    Here is an example webpack.config.js:

    { 
        test: /.jsx?$/,         // Match both .js and .jsx files
        exclude: /node_modules/, 
        loader: "babel", 
        query:
          {
            presets:['react']
          }
    }
    

    Recently Babel 6 was released and there was a major change: https://babeljs.io/blog/2015/10/29/6.0.0

    If you are using react 0.14, you should use ReactDOM.render() (from require('react-dom') ) instead of React.render() : https://facebook.github.io/react/blog/#changelog

    UPDATE 2018

    Rule.query has already been deprecated in favour of Rule.options. Usage in webpack 4 is as follows:

    npm install babel-loader babel-preset-react
    

    Then in your webpack configuration (as an entry in the module.rules array in the module.exports object)

    {
        test: /.jsx?$/,
        exclude: /node_modules/,
        use: [
          {
            loader: 'babel-loader',
            options: {
              presets: ['react']
            }
          }
        ],
      }
    

    I ran into a similar issue when migrating from babel 5 to babel 6.

    I was just running babel to compile the src to lib folder babel src --out-dir lib

    I will share my setup for babel 6:

    Ensure you have the following babel 6 devDependencies installed

    "babel-core": "^6.7.6",
    "babel-loader": "^6.2.4",
    "babel-preset-es2015": "^6.6.0",
    "babel-preset-react": "^6.5.0",
    "babel-preset-stage-0": "^6.5.0"
    

    Add your .babelrc file to the project:

    {
      "presets": ["es2015", "stage-0", "react"]
    }
    

    由于上面的答案仍然让一些人处于黑暗中,因此完整的webpack.config.js可能如下所示:

    var path = require('path');
    var config = {
        entry: path.resolve(__dirname, 'app/main.js'),
        output: {
            path: path.resolve(__dirname, 'build'),
            filename: 'bundle.js'
        },
        module: {
            loaders: [{
                test: /.jsx?$/,
                loader: 'babel',
                query:
                {
                    presets:['es2015', 'react']
                }
            }]
        },
    
    };
    
    module.exports = config;
    链接地址: http://www.djcxy.com/p/46034.html

    上一篇: 我如何将SmartyStreets验证应用于同一页面中的两种表单?

    下一篇: loader jsx SyntaxError:意外的令牌