Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Babel loader error: rest/spread operator not understood

When I try to run my webpack bundle, I get this console error:

BabelLoaderError: SyntaxError: Unexpected token (113:19)

  111 | 
  112 | const render = () => ReactDOM.render(
> 113 |    <TodoApp todos={...store.getState()}/>,
      |                    ^
  114 |    document.getElementById("root")
  115 | )
  116 | 

So the spread operator syntax is not understood. However, I have installed the babel-stage-2 preset which I have included in my loaders in webpack.config.js, as follows:

module: {
        loaders: [
            {
               test: path.join(__dirname, 'public'),
               loader: ['babel-loader'],
               query: {
                 presets: debug ? ['react', 'es2015', 'react-hmre', 'stage-2'] : ['react', 'es2015', 'stage-2']
               }
            }
        ]
    }

I also tried installing babel-plugin-transform-object-rest-spread and adding to .babelrc:

{
  "plugins": ["transform-object-rest-spread"]
}

Still receiving the same error.

Any help appreciated!

like image 551
Paulos3000 Avatar asked Mar 09 '26 23:03

Paulos3000


1 Answers

I have a similar issue with a Vue project.

I have added:

{
  "presets": [
    ["es2015", {"modules": false}],
    ["stage-2"]
  ],
  "plugins": ["transform-object-rest-spread"]
}

in .babelrc and tried many things, but nothing seems to work.

As mentioned in preset-env plugin in caveats section, you should have at least v6.19.0, mine is v6.23.0 and it still doesn't work.

EDIT: I've found the Answer here.

MY SOLUTION

/*
 * additional javascript loader for es6 code in node_modules that have to be transpiled also
 */
{
    test: /\.jsx?$/,
    include: [
        NODE_MODULES + '/vuetify/src'
    ],
    use: [
        {
            loader: 'babel-loader',
            options: Config.babel()
        }
    ]
}
like image 197
CodeXP Avatar answered Mar 11 '26 12:03

CodeXP



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!