1

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.

1

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

ermagod... my dyslexia... ugh. Thanks.
How is it that messing a filename results in an error on the content ? What is the processing that webpack follows when not given any config file ?
@Titou webpack uses a default config if it can't find a config file. The default path is src/index.js so it found the file. But the default config does not compile with babel, so it couldn't read the jsx

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.