reactjswebpackonsen-uionsen-ui2

ons-navigator: Error: Module parse failed: Unexpected token (330:6)


How to fix this error? This error is occurs when using the Navigator component from 'react-onsenui' lib.

Error: Module parse failed: Unexpected token (330:6)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
| 
|     return (
>       <ons-navigator { ...attrs } ref={(navi) => { this._navi = navi; }}>
|         {pages}
|       </ons-navigator>

Part of Webpack.config.js code:

module: {
    rules: [
      {
        test: /\.(js|jsx)$/,
        exclude: /node_modules/,
        include: path.resolve(__dirname, 'src'),
        use: [{
          loader: 'babel-loader',
          options: {
            presets: ['react', 'env'],
            plugins: [
              'react-hot-loader/babel',
              'transform-class-properties',
              'transform-object-rest-spread'
            ]
          }
        }]
      }
...

Solution

  • The reason for this error is incorrect import of Navigator component.
    Wrong: import Navigator from "react-onsenui/src/components/Navigator";
    Correct: import {Navigator} from 'react-onsenui';

    The wrong import has been added after force auto-import by IDE.