Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compile error with dynamic import : Module parse failed: Unexpected token #11

Open
gloriaJun opened this issue May 7, 2019 · 0 comments

Comments

@gloriaJun
Copy link
Owner

gloriaJun commented May 7, 2019

Version

"webpack": "^4.29.0",
"@babel/cli": "^7.2.3",
"@babel/core": "7.0.0",
"@babel/plugin-transform-runtime": "^7.2.0",
"@loadable/component": "^5.9.0",
"react": "^16.8.6",
"react-dom": "^16.8.6",
"react-router-dom": "^5.0.0",

npm v 6.7.0
node v11.12.0

Test Scenario

import React from 'react';
import { Redirect, Route, Switch } from 'react-router-dom';
import lazy from '@loadable/component';

const Top = lazy(() => import('@components/Top'));
const Login = lazy(() => import('@components/Login'));
const SignUp = lazy(() => import('@components/SignUp'));
const PinCode = lazy(() => import('@components/common/PinCode'));
const DebitCard = lazy(() => import('@components/DebitCard'));
const TermDeposit = lazy(() => import('@components/TermDeposit'));

const App = () => {
  return (
    <Switch>
      <Route path="/login" component={Login} />
      <Route path="/signup" component={SignUp} />
      <Route path="/termdeposit" component={TermDeposit} />
      <Route path="/debitcard" component={DebitCard} />
      <Route path="/pincode" component={PinCode} />

      <Route path="/" component={Top} />
      <Redirect exact from="/" to="/" />
    </Switch>
  );
};

export default App;

Error Log

ERROR in ./src/components/App.js 5:9
Module parse failed: Unexpected token (5:9)
You may need an appropriate loader to handle this file type.
| import lazy from '@loadable/component';
| var Top = lazy(function () {
>   return import('@components/Top');
| });
| var Login = lazy(function () {
 @ ./src/client/client.js 9:0-34 18:66-69

Cause

Solution

For solving this problem, you can have three options.

  • using yarn instead of npm.
  • Install acorn
  1. add acorn@^6.1.1
yarn add -D acorn@^6.1.1
# or 
npm install -D acorn@^6.1.1
  1. clean previous package history
rm -rf node_modules yarn.lock package-lock.json

Install package

Reference

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant