question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

SyntaxError: Unexpected token import

See original GitHub issue

💬 Questions and Help

I am following the guide for the SSR, I arrived at the moment to split in chunks my views.

import loadable from '@loadable/component';
const App = loadable(() => import('../components/App'));
const Home = loadable(() => import('../components/Home'));
const About = loadable(() => import('../components/About'));

export default [
  {
    ...App,
    routes: [
      {
        ...Home,
        path: '/',
        exact: true
      },
      {
        ...About,
        path: '/about'
      }
    ]
  }
];

My first doubt was if I need to install @babel/plugin-syntax-dynamic-import or it is not needed. But I get a runtime error in both cases

return import(… SyntaxError: Unexpected token import

I guess it is because babel is not working well, but if i remove this the loadable components is creating just one build (like it should be).

Above my .babelrc

  "presets": [
    "@babel/preset-env",
    "@babel/preset-react"
  ],
  "plugins": [
    "@loadable/babel-plugin",
    // "@babel/plugin-syntax-dynamic-import",
    "@babel/plugin-transform-runtime",
    "@babel/plugin-proposal-export-default-from",
    "@babel/plugin-proposal-export-namespace-from"
  ]
}

My pacakeges:

     "@babel/cli": "^7.2.3",
    "@babel/core": "^7.2.2",
    "@babel/node": "^7.2.2",
    "@babel/plugin-proposal-export-default-from": "^7.2.0",
    "@babel/plugin-proposal-export-namespace-from": "^7.2.0",
    "@babel/plugin-syntax-dynamic-import": "^7.2.0",
    "@babel/plugin-transform-runtime": "^7.2.0",
    "@babel/preset-env": "^7.2.3",
    "@babel/preset-react": "^7.0.0",
    "@loadable/babel-plugin": "^5.2.2",
    "@loadable/component": "^5.2.2",
    "@loadable/webpack-plugin": "^5.2.2",
    "@loadable/server": "^5.3.0",
    "babel-eslint": "^10.0.1",
    "babel-loader": "^8.0.5",

I run my server like this: "serve": "NODE_ENV=development nodemon --exec babel-node --inspect src/server/index.js",

Any ideas?

thanks!

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:19 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
gregbergecommented, Mar 24, 2019

As I said, if you have to add babel-plugin-dynamic-import-node, it is because your dynamic import is called server-side. It should not be the case with Loadable Components, on server-side it does a synchronous call.

1reaction
ericgiocommented, Mar 23, 2019

I had a similar issue while trying to use SSR and the problem for me was that node couldn’t handle dynamic imports. I tried a few solutions, including adding babel-plugin-dynamic-import-webpack to my .babelrc. That sort of worked, but caused other problems. What finally worked for me was adding babel-plugin-dynamic-import-node only to my @babel/register call:

// In my index.js, which is called by node...
require('@babel/register')({
  plugins: [
    'dynamic-import-node',
  ]
});

Note that adding this plugin to my .babelrc did not work, since both my server- and client-side code call that. With the above, only node is affected.

I’m honestly kind of surprised more people haven’t run into this, though I see at least one other issue mentioning the problem and solution. @neoziro: it might be worth mentioning this plugin in the SSR docs?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Node.js - SyntaxError: Unexpected token import - Stack Overflow
Node.js - SyntaxError: Unexpected token import · 4. Use transpiler like Babel to use import in Nodejs as it is not natively supported...
Read more >
SyntaxError: Unexpected token import in Node.js | bobbyhadz
The "SyntaxError: Unexpected token import" occurs when we use the ES6 import syntax in a version of Node that doesn't support it. To...
Read more >
Unexpected token import Node.js - Reactgo
In this tutorial, we are going to learn about how to resolve the unexpected token import error in Node.js.
Read more >
Nodejs Uncaught SyntaxError: Unexpected token import
An unexpected token import occurs when an error message appears in the console while running a web application.
Read more >
Error Unexpected token import in nodejs | Edureka Community
SyntaxError : Unexpected token import at exports.runInThisContext (vm.js:53:16) at Module._compile (module.js:387:25) at Object.Module.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found