SyntaxHighlighter with webpack and react
Asked Answered
W

1

7

I try to implement SyntaxHighlighter using react and webpack.

I install it via npm.

npm install --save syntaxhighlighter

It install perfectly. Now the problem is how to import it properly.

I've tried like this:

import SyntaxHighlighter from 'syntaxhighlighter';

But it doesn't work, Chrom console reports the error like:

TypeError: Cannot assign to read only property 'exports' of object '#'

How can I use syntaxHighlighter with webpack and react?

Waggoner answered 2/8, 2017 at 5:20 Comment(0)
H
3

Step 1: Install react-syntax-highlighter

npm install react-syntax-highlighter

Step 2: Import react-syntax-highlighter

import SyntaxHighlighter from "react-syntax-highlighter";

Step 3: Import a style

import { aStyle } from "react-syntax-highlighter/styles/hljs-or-prism";

Step 4: Display your code

<SyntaxHighlighter language="yourLanguage" style={aStyle}>
     {yourCode}
</SyntaxHighlighter>

Example

import React from "react";
import ReactDOM from "react-dom";

import SyntaxHighlighter from "react-syntax-highlighter";
import { docco } from "react-syntax-highlighter/styles/hljs";

function App() {
    return (
        <div className="App">
            <SyntaxHighlighter language="javascript" style={docco}>
                {console.log('Hello world!...');}
            </SyntaxHighlighter>
        </div>
    );
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);

Live running example

Full documentation

Hitherto answered 28/8, 2018 at 9:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.