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:
- Created 5 years ago
- Comments:19 (7 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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.
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 addingbabel-plugin-dynamic-import-node
only to my @babel/register call: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?