Unexpected token 'export' when used in NextJs
See original GitHub issueWhen I import Themes inside NextJs application from themes directory(‘react-syntax-highlighter/dist/cjs/styles/hljs’) as:
import { atomOneDark, atomOneLight } from "react-syntax-highlighter/dist/cjs/styles/hljs";
It throws a syntax ERROR as
Unexpected token 'export'
I looked for some solution from Internet, at tried dynamic import as:
import dynamic from 'next/dynamic'
const { atomOneDark, atomOneLight } = dynamic(
() => import('react-syntax-highlighter/dist/cjs/styles/hljs'),
{ ssr: false }
)
And,
import dynamic from 'next/dynamic'
const atomOneDark = dynamic(
() => import('react-syntax-highlighter/dist/cjs/styles/hljs/atom-one-dark'),
{ ssr: false }
)
Still not solved.
Then I tried to simply import individual theme directly from theme file, fortunately it works for me;
import atomOneDark from "react-syntax-highlighter/dist/cjs/styles/hljs/atom-one-dark";
import atomOneLight from "react-syntax-highlighter/dist/cjs/styles/hljs/atom-one-light";
Please have a look at it.
Issue Analytics
- State:
- Created 2 years ago
- Reactions:7
- Comments:6
Top Results From Across the Web
Next.js SyntaxError "Unexpected token 'export'" - Stack Overflow
I was getting the same error: Unexpected token 'export' ; with a node_modules dependency made for the browser (client-side).
Read more >SyntaxError: Unexpected token 'export' #31518 - vercel/next.js
Nextjs is failing on build of a third party package. The third party package is using ES6 syntax for exports. export { Root...
Read more >Fixing "SyntaxError: Unexpected token 'export'" with NextJS
Fixing “SyntaxError: Unexpected token ”export”” with NextJS. I ran into an error when trying to use reactgrid with NextJS. The error was
Read more >[Solved] SyntaxError: Unexpected token '.' Jest Error for Next.js ...
This error could be caused by a number of issues and is related to Jest wanting to process CommonJS code. But it is...
Read more >SyntaxError: Unexpected token 'export' in ScrollTrigger (Next.js)
Hello, I've used similar code in react before, but for some reason in Next.js, even simply importing ScrollTrigger causes this error.
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 Free
Top 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
I’ve used the commonjs modules instad of esmodules and it worked just fine ! just make sure not using the cjs modules like esmodules
instand use them like this
Forget it, I used the example from the library importing
source: npmjs/package/react-syntax-highlighter
Prism
and it worked