Import React from 'react' results in Uncaught SyntaxError: Unexpected identifier

adang3

I've installed webpack 3 along with babel and my entry index.js/bundle.js will build and run, which I've tested with ES7/8 features, however imports won't work and result in Uncaught SyntaxError: Unexpected identifier. I've tried putting the babel config in the package.json as well as in a separate .babelrc file in my app root directory but I still get the error when trying to import. Am I missing a package or setting?

index.js (works)

// does not work
// import React from 'react' 

// works
const object1 = {
  a: 'somestring',
  b: 42,
  c: false
};

console.log(Object.values(object1));

webpack.config.js

const path = require('path')

module.exports = {
  context: path.resolve(__dirname, 'app/assets/javascripts/source'),
  entry: './index.js',
  output: {
    filename: 'bundle.js',
    path: path.resolve(__dirname, 'app/assets/javascripts/webpack')
  },
  module: {
    rules: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        use: {
          loader: 'babel-loader',
          options: {
            presets: ['@babel/preset-env']
          }
        }
      }
    ]
  }
}

.babelrc

{
  "presets": ["env", "react"]
}

package.json

}
  ...
  "license": "MIT",
  "scripts": {
    "build": "webpack",
  },
  "babel": {
    "presets": [
      "env",
      "react"
    ]
  },
  "devDependencies": {
    "babel-core": "^6.26.0",
    "babel-loader": "^7.1.2",
    "babel-preset-env": "^1.6.1",
    "babel-preset-react": "^6.24.1",
    "prop-types": "^15.6.0",
    "react": "^16.2.0",
    "react-dom": "^16.2.0",
    "webpack": "^3.10.0",
    "webpack-dev-server": "^2.11.1"
  }
}
Jayavel

Try this : transform-es2015-modules-amd , This plugin transforms ES2015 modules to Asynchronous Module Definition (AMD).

{
    presets: ["env", "react"],
    plugins: ["transform-es2015-modules-amd"]
 }

more at transform-es2015-modules-amd

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

ES6 module Import giving "Uncaught SyntaxError: Unexpected identifier"

Uncaught SyntaxError:` Unexpected token import

"Uncaught SyntaxError: Unexpected identifier"

Import Unexpected identifier + SyntaxError: Unexpected string

Firebase: Uncaught SyntaxError: Unexpected identifier

React-Table giving an Uncaught SyntaxError: Unexpected Identifier in propTypes.js

Script error : "Uncaught SyntaxError: Unexpected identifier"

import React from "react"; resulting in Uncaught SyntaxError: Unexpected identifier

Uncaught SyntaxError: Unexpected identifier (imported class in javascript)

Angular app causes Uncaught SyntaxError: Unexpected identifier on import-from in JavaScript

create-react-app SyntaxError: Unexpected identifier when running a test

Using import statement, why does it fail with "Uncaught SyntaxError: Unexpected identifier""

Uncaught SyntaxError: Unexpected token < with React

Uncaught SyntaxError: Unexpected identifier with import statement

React/Node Uncaught SyntaxError: Unexpected token <

How to import a variable from a JS file in another JS file that uses Vue CDN? - Uncaught SyntaxError: Unexpected identifier

SyntaxError: Unexpected identifier import bodyParser from 'body-parser'; … In REACT JS app

Uncaught SyntaxError: Unexpected identifier at compute

SyntaxError: Unexpected identifier importing React (Javascript)

Uncaught SyntaxError: Unexpected identifier in import Echo from 'laravel-echo' js file present in resources/js/bootstrap.js

SyntaxError: Unexpected identifier while deploying React application on Docker container

Uncaught SyntaxError: Unexpected identifier IN Chrome

Embeddin PHP into Javascript ,Uncaught SyntaxError: Unexpected identifier

Uncaught SyntaxError: Unexpected identifier

Javascript Uncaught SyntaxError: Unexpected identifier

React fetch results in Uncaught (in promise) SyntaxError: Unexpected end of JSON input

Uncaught SyntaxError: Unexpected identifier in html

Javascript import class from file yields "Uncaught SyntaxError: Unexpected Identifier"

Node js Uncaught SyntaxError: Unexpected identifier