LuiGiovanni LuiGiovanni - 3 years ago 249
Node.js Question

Getting an uxepected token error using reactjs and nodejs

When creating a simple button that counts the clicks i get the unexpected token error on the button tag for (what i can tell) no reason

import React from 'react';

/**
* A counter button: tap the button to increase the count.
*/
class Counter extends React.Component {
constructor() {
super();
this.state = {
count: 0,
};
}

render() {
return (
<button onClick={() => {this.setState({ count: this.state.count + 1 });}}>
Count: {this.state.count}
</button>
);
}
}
export default Counter;

Answer Source

You might be missing presets in webpack.config.js. Try this

module : {
        rules:[
            { 
                test: /\.(js|jsx)$/,
                exclude: /node_modules/,
                use : {
                    loader: "babel-loader",
                    options: {
                        presets: ['env','react']
                    }
                }
            }
        ]
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download