Homan
Homan

Reputation: 26718

Module parse failed: Unexpected token using webpack-dev-server

I'm trying to follow along the examples in the Learning Redux book. Unfortunately the latest versions of webpack and babel have all changed and I updated to the latest versions which deviate from the configurations in the book.

I've read the latest documentation from both babel and webpack and I believe I have the correct .babelrc and config.webpack.js that is appropriate.

But I'm struggling to get jsx to compile with the webpack-dev-server. I get this error when running npm start:

ERROR in ./src/index.js 
Module parse failed: Unexpected token (10:4) 
You may need an appropriate loader to handle this file type. 
| 
| ReactDOM.render( 
|     <h1>hello world!</h1>, 
|   document.getElementById('root') 
| )  
@ multi (webpack)-dev-server/client?http://localhost:8080 ./src

Here is my full setup: https://github.com/homanchou/learning_redux

package.json

{
 "name": "learningredux",
  "version": "1.0.0",
  "description": "",
  "main": "src/index.js",
  "scripts": {
    "start": "webpack-dev-server --open --mode development",
    "build": "webpack --mode production",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "babel-cli": "^6.26.0",
    "babel-core": "^6.26.3",
    "babel-loader": "^7.1.4",
    "babel-plugin-transform-object-rest-spread": "^6.26.0",
    "babel-preset-env": "^1.6.1",
    "babel-preset-react": "^6.24.1",
    "eslint": "^4.19.1",
    "eslint-plugin-react": "^7.7.0",
    "webpack": "^4.6.0",
    "webpack-cli": "^2.1.2",
    "webpack-dev-server": "^3.1.3"
  },
  "dependencies": {
    "npm": "^6.0.0",
    "react": "^16.3.2",
    "react-dom": "^16.3.2",
    "redux": "^4.0.0"
  }
}

babelrc.

{
 "presets": ["env", "react"],
 "plugins": [ "transform-object-rest-spread" ]
}

config.webpack.js

const path = require('path')
module.exports = {
    entry: './src/index.js',
    output: {
        path: path.resolve('dist'),
        filename: 'main.js'
    },
    module: {
        rules: [{
            test: /\.js$/,
            exclude: /node_modules/,
            use: {
              loader: "babel-loader"
            }
        }]
    }
}

Any help is appreciated.

Upvotes: 1

Views: 1021

Answers (1)

lukas-reineke
lukas-reineke

Reputation: 3322

You just mixed the order in the config.webpack.js filename. It needs to be webpack.config.js

Or you need to run webpack with --config and pass the filename.

Everything else should work.

Upvotes: 2

Related Questions