Type error while trying to use marked module

97 views Asked by At
const marked = require('marked');
articleSchema.pre("validate", function(next) {
    if (this.title){
        this.slug = slugify(this.title, {lower: true, strict: true})
    }
    if(this.markdown){
        this.sanitaizeHTML = marked(this.markdown);
    }
    next();
})

When I try to use marked module as a function I will return typeError

TypeError: marked is not a function at model. (C:\Users\araiz\OneDrive\Desktop\Test_001\models\article.js:42:30) at callMiddlewareFunction (C:\Users\araiz\OneDrive\Desktop\Test_001\node_modules\kareem\index.js:628:27) at next (C:\Users\araiz\OneDrive\Desktop\Test_001\node_modules\kareem\index.js:93:7) at Kareem.execPre (C:\Users\araiz\OneDrive\Desktop\Test_001\node_modules\kareem\index.js:122:8) at Kareem.wrap (C:\Users\araiz\OneDrive\Desktop\Test_001\node_modules\kareem\index.js:368:8) at model.$__validate (C:\Users\araiz\OneDrive\Desktop\Test_001\node_modules\kareem\index.js:502:11) at C:\Users\araiz\OneDrive\Desktop\Test_001\node_modules\mongoose\lib\document.js:2576:10 at new Promise () at model.validate (C:\Users\araiz\OneDrive\Desktop\Test_001\node_modules\mongoose\lib\document.js:2575:10) at model.validateBeforeSave (C:\Users\araiz\OneDrive\Desktop\Test_001\node_modules\mongoose\lib\plugins\validateBeforeSave.js:35:12)

1

There are 1 answers

0
Maizied Hasan Majumder On

The error you're getting is saying that marked is not a function. This usually happens if the required module was not imported properly or the module itself is not exporting a function as expected.

There could be a few reasons for this:

  1. The marked package is not installed. You can install it using npm install marked.

  2. You have a different version of the marked package that might not be compatible. Try uninstalling and reinstalling the package:

npm uninstall marked
npm install marked
  1. There could be some issues with your node_modules folder. Try deleting it and reinstalling your packages:
rm -rf node_modules
npm install
  1. There might be some confusion in how you're trying to use marked. marked should be a function if properly imported. It converts Markdown into HTML. Make sure you're using it correctly:
const marked = require('marked');

// Use it as a function
console.log(marked('# Marked in Node.js\n\nRendered by **marked**.'));

Make sure to check if any of these solutions work for you. If not, please provide more information and we can find a more detailed solution.