5

When I render the code below I get this error:

SyntaxError: Unexpected token 'export'

(project path)/node_modules/react-syntax-highlighter/dist/esm/styles/prism/index.js

It initially works as intended until I refresh the page.

import { Prism as SyntaxHighlighter } from "react-syntax-highlighter";
import { oneLight } from "react-syntax-highlighter/dist/esm/styles/prism";

const codeString = '(num) => num + 1';

const Code = () => {
  return (
    <div>
      <SyntaxHighlighter language="javascript" style={oneLight}>
        {codeString}
      </SyntaxHighlighter>
    </div>
  );
};

export default Code;
0

1 Answer 1

16

Solution for Unexpected token 'export' in Next.js when using react-syntax-highlighter

Use this import path:

react-syntax-highlighter/dist/cjs/...

instead of:

react-syntax-highlighter/dist/esm/...
Sign up to request clarification or add additional context in comments.

1 Comment

I understood this has to do with CommonJS and ES Modules but why only react-syntax-highlighter is throwing errors? Are any settings to be toggled inside Next.js or is it just the package thing?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.