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.

Unexpected token 'export' when used in NextJs

See original GitHub issue

When 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:open
  • Created 2 years ago
  • Reactions:7
  • Comments:6

github_iconTop GitHub Comments

7reactions
triyanoxcommented, Feb 15, 2022

I’ve used the commonjs modules instad of esmodules and it worked just fine ! just make sure not using the cjs modules like esmodules

{your module}

instand use them like this

import { Prism as SyntaxHighlighter } from "react-syntax-highlighter";
import { materialDark } from "react-syntax-highlighter/dist/cjs/styles/prism";
import { materialLight } from "react-syntax-highlighter/dist/cjs/styles/prism";
import { ThemeContext } from "../Themes/ThemeContext";
import { useContext } from "react";

function highlighter(props) {
  const { theme, setTheme } = useContext(ThemeContext);

  return (
    <SyntaxHighlighter
      language={props.language}
      style={theme === "dark" ? materialDark : materialLight}
    >
      {props.children}
    </SyntaxHighlighter>
  );
}
export default highlighter;
2reactions
mrLuisFercommented, Jan 7, 2022

I try what @tlamadon comments and it works, it no longer shows the error, but it doesnt highlight it correctly

code preview

or so it seems to me website preview

Forget it, I used the example from the library importing Prism and it worked code preview but it works xD source: npmjs/package/react-syntax-highlighter

Read more comments on GitHub >

github_iconTop 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 >

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