最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Marked is not a function - Stack Overflow

programmeradmin3浏览0评论

I'm trying to build a markdown previewer using react and the marked node package. Everything works up to when I try to parse any markdown on the DOM. When I do I get an error message: Uncaught TypeError: marked is not a function.

I have marked declared at the top of the file like this:

import { useState } from "react";
import "./App.css";
const marked = require("marked");

I also tried declaring marked like this:

import { useState } from "react";
import "./App.css";
const { marked } = require("marked");

But this didn't seem make a difference.

This is my code configuration:

            <div className="preview-label">Preview</div>
            <div
              id="preview"
              className="preview-area"
              dangerouslySetInnerHTML={{
                __html: marked(mrkdwn),
              }}
            ></div>
          </div>

I've also tried:

            <div className="preview-label">Preview</div>
            <div
              id="preview"
              className="preview-area"
              dangerouslySetInnerHTML={{
                __html: marked.parse(mrkdwn),
              }}
            ></div>
          </div>
            <div className="preview-label">Preview</div>
            <div
              id="preview"
              className="preview-area"
              dangerouslySetInnerHTML={{
                __html: marked.parser(mrkdwn),
              }}
            ></div>
          </div>

and

            <div className="preview-label">Preview</div>
            <div
              id="preview"
              className="preview-area"
              dangerouslySetInnerHTML={{
                __html: marked.Parser(mrkdwn),
              }}
            ></div>
          </div>

However, I've also gotten similar error messages with these attempts as well, only in these cases the messages say .Parser, .parse, .parser, etc., is not a function.

If you need to see anymore code, please let me know.

I haven't been able to find a solution in any of my Googling. It's possible there's something out there I missed.

Any help would be greatly appreciated.

Thank you in advance.

I'm trying to build a markdown previewer using react and the marked node package. Everything works up to when I try to parse any markdown on the DOM. When I do I get an error message: Uncaught TypeError: marked is not a function.

I have marked declared at the top of the file like this:

import { useState } from "react";
import "./App.css";
const marked = require("marked");

I also tried declaring marked like this:

import { useState } from "react";
import "./App.css";
const { marked } = require("marked");

But this didn't seem make a difference.

This is my code configuration:

            <div className="preview-label">Preview</div>
            <div
              id="preview"
              className="preview-area"
              dangerouslySetInnerHTML={{
                __html: marked(mrkdwn),
              }}
            ></div>
          </div>

I've also tried:

            <div className="preview-label">Preview</div>
            <div
              id="preview"
              className="preview-area"
              dangerouslySetInnerHTML={{
                __html: marked.parse(mrkdwn),
              }}
            ></div>
          </div>
            <div className="preview-label">Preview</div>
            <div
              id="preview"
              className="preview-area"
              dangerouslySetInnerHTML={{
                __html: marked.parser(mrkdwn),
              }}
            ></div>
          </div>

and

            <div className="preview-label">Preview</div>
            <div
              id="preview"
              className="preview-area"
              dangerouslySetInnerHTML={{
                __html: marked.Parser(mrkdwn),
              }}
            ></div>
          </div>

However, I've also gotten similar error messages with these attempts as well, only in these cases the messages say .Parser, .parse, .parser, etc., is not a function.

If you need to see anymore code, please let me know.

I haven't been able to find a solution in any of my Googling. It's possible there's something out there I missed.

Any help would be greatly appreciated.

Thank you in advance.

Share Improve this question edited Feb 21, 2023 at 9:37 ccpizza 31.7k23 gold badges181 silver badges185 bronze badges asked Jan 9, 2022 at 21:55 Holger MuellerHolger Mueller 4321 gold badge7 silver badges17 bronze badges 1
  • 1 Where is marked declared? It appears to be undefined in your snippets. stackoverflow.com/help/minimal-reproducible-example – Drew Reese Commented Jan 9, 2022 at 22:53
Add a comment  | 

2 Answers 2

Reset to default 16

I found that I'm supposed to import marked like this:

import { marked } from 'marked'

this was what worked for me.

const { marked } = require('marked');
发布评论

评论列表(0)

  1. 暂无评论