Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
708 views
in Technique[技术] by (71.8m points)

javascript - webpack: syntax error with babel-loader

I'm starting to learn react with a tutorial. But webpack is not working as expected.

So here is my simple webpack.conf.js file.

module.exports = {
    entry: "./app-client.js",
    output: {
        filename: "public/bundle.js"
    },
    module: {
        loaders: [
            {
                exclude: /(node_modules|app-server.js)/,
                loader: 'babel'
            }
        ]
    }
};

Also I installed all the modules:

npm install -g webpack
npm install webpack react babel-loader babel-core

But when running webpack, I got the following error message:

ERROR in ./app-client.js
Module build failed: SyntaxError: app-client.js: Unexpected token (4:13)
  2 | var APP = require('./components/APP');
  3 | 
> 4 | React.render(<APP />, document.getElementById('react-container'));
    |              ^

In my understanding, babel-loader is supposed to take care of that. But it looks like it's not making the effort.

What am I missing?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Babel 6 doesn't do anything by itself. In order to properly process JSX, you need to have the following in your .babelrc file:

{
    "presets": ["react"]
}

Also, you need to make sure you install that preset using NPM:

$ npm install --save-dev babel-core react react-dom babel-preset-react

A good place to start is the official React getting started page


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...