Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
176
rated 0 times [  180] [ 4]  / answers: 1 / hits: 97967  / 13 Years ago, tue, october 11, 2011, 12:00:00

When I open console of Chrome 14 and type...



require (or require(), if that matters)



I get: ReferenceError.



This means that JavaScript doesn't have that function by default, right? At least on web browsers.



Why I'm talking about that?

I needed Markdown parser for JavaScript.



What to do?

I, as usually, opened GitHub and searched for it. The first results that matched my needs was this and this.



Usually (I'm not that good with JavaScript) I include script I want to use before my code using <script /> tag and then... well - use it. But this time I don't get what's happening... :(



Usage for #1 script:



var input = # HeadingnnParagraph;
var output = require( markdown ).toHTML( input );
print( output );


Usage for #2 script:



var marked = require('marked');
console.log(marked('i am using __markdown__.'));


Where does that require() came from? Thanks in an advice! :)


More From » function

 Answers
18

It's a way to include node.js packages. Luckily, the first package you linked to, markdown-js, is very smart. It checks whether it is included as a node package, and if not, will set the markdown object to window.markdown. So all you have to do is include this file in a <script> tag and you should be able to use the markdown object from the global scope.


[#89676] Monday, October 10, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
brodyfrancisi

Total Points: 1
Total Questions: 102
Total Answers: 89

Location: Marshall Islands
Member since Mon, May 31, 2021
3 Years ago
brodyfrancisi questions
;