Search code examples
javascriptnode.jsrenderingrendermustache

Mustache is not defined error in node js : Uncaught error


I want to render the pages on the client side with Mustache.js but it says Mustache is not defined. I found an answer here but it's difficult to understand as i just started learning about websites/rendering etc. In order to learn i need to fix this issue but i have no clue how can i do that.

Can someone tell me what do i need to do to fix it without going into much details as i am a beginner in scripting languages.

Here is my code: I am trying to open a new page and then simple render it.

socket.on('sign-up-succes',(data) => {
  window.open(data.redirect,"_self");
  var template = jQuery('#message-template').html();
  var html = Mustache.render(template,{
    text : "Hello"
   });
  jQuery('#messages').append(html);
});

Solution

  • From the code you posted I see you are using window object, so I believe you are trying to use Mustache in the client side script.

    If so, then use below CDN:

    https://cdnjs.cloudflare.com/ajax/libs/mustache.js/3.0.0/mustache.js
    

    or

    https://cdnjs.cloudflare.com/ajax/libs/mustache.js/3.0.0/mustache.min.js
    

    I suggest you render templates from Server side(Node.js in your case).