Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

babel-preset-env not recognizing jsx

I'm trying to do a webpack build for the first time and I have looked over my babel config a bunch of times relative to the docs but I cannot for the life of me see where it's wrong. All the same I am getting an error on build of a pretty simple app (almost the default app, with redux hooked up):

ERROR in ./src/index.js
Module build failed: SyntaxError: Unexpected token (10:2)
   8 | 
   9 | ReactDOM.render(
> 10 |   <Provider store={store}>
     |   ^
  11 |     <App />
  12 |   </Provider>, global.document.getElementById('root'));
  13 | registerServiceWorker();

Here is my webpack.config.js:

const path = require('path');
const UglifyJSPlugin = require('uglifyjs-webpack-plugin');

module.exports = {
  entry: './src/index.js',
  plugins: [
    new UglifyJSPlugin()
  ],
  output: {
    filename: 'bundle.js',
    path: path.resolve(__dirname, 'dist')
  },
  module: {
    rules: [
      {
        test: /\.css$/,
        use: [
          'style-loader',
          'css-loader'
        ]
      },
      {
        test: /\.(png|svg|jpg|gif)$/,
        use: [
          'file-loader'
        ]
      },
      {
        test: /\.(js|jsx)$/,
        exclude: /(node_modules)/,
        use: [
          'babel-loader'
        ]
      }
    ]
  }
};

also Ive used a variation suggested by the webpack docs:

{
  test: /\.js$/,
  exclude: /(node_modules|bower_components)/,
  use: {
    loader: 'babel-loader',
    options: {
      presets: ['env']
    }
  }
}

and my package.json:

{
  "name": "interactor",
  "version": "0.1.0",
  "private": true,
  "babel": {
    "presets": [
      ["env", {
        "targets": {
          "browsers": ["last 2 versions"]
        }
      }]
    ]
  },
  "dependencies": {
    "react": "^16.0.0",
    "react-bootstrap": "^0.31.3",
    "react-dom": "^16.0.0",
    "react-redux": "^5.0.6",
    "react-scripts": "1.0.14",
    "redux": "^3.7.2",
    "redux-thunk": "^2.2.0"
  },
  "scripts": {
    "start": "webpack --config webpack.config.js",
    "build": "webpack",
    "test": "react-scripts test --env=jsdom"
  },
  "devDependencies": {
    "babel-cli": "^6.26.0",
    "babel-core": "^6.26.0",
    "babel-loader": "^7.1.2",
    "babel-preset-env": "^1.6.0",
    "css-loader": "^0.28.7",
    "eslint": "^4.8.0",
    "eslint-config-airbnb": "^15.1.0",
    "eslint-plugin-import": "^2.7.0",
    "eslint-plugin-jsx-a11y": "^5.1.1",
    "eslint-plugin-react": "^7.4.0",
    "file-loader": "^1.1.5",
    "style-loader": "^0.19.0",
    "uglifyjs-webpack-plugin": "^0.4.6",
    "webpack": "^3.6.0"
  }
}

and I guess index.js might be useful too:

import React from 'react';
import ReactDOM from 'react-dom';
import { Provider } from 'react-redux';
import store from './store';
import './index.css';
import App from './App';
import registerServiceWorker from './registerServiceWorker';

ReactDOM.render(
  <Provider store={store}>
    <App />
  </Provider>, global.document.getElementById('root'));
registerServiceWorker();

What am I doing wrong here?

like image 430
TrivialCase Avatar asked Oct 06 '17 20:10

TrivialCase


1 Answers

As usual, user error, but maybe it will help another newb like me down the line. Just never put "react" into my babel presets. It should have looked, e.g., like

"babel": {
    "presets": [
      "react",
      ["env", {
        "targets": {
          "browsers": ["last 2 versions"]
        }
      }]
    ]
  }

Oh, and make sure you actually have babel-preset-react installed.

like image 173
TrivialCase Avatar answered Sep 28 '22 14:09

TrivialCase