troubleshooting Question

what does require.js do

Avatar of Rohit Bajaj
Rohit BajajFlag for India asked on
Web ApplicationsJavaScriptJava EEjQuery
1 Comment1 Solution185 ViewsLast Modified:
In my web application inside a jsp there is a line :
   <script data-main="/code/resources/js/new.js" src="/code/resources/lib/require.js"></script>

what does this line means. And whats the use of require.js
I read somwhere that it is used for optimization. ?

Also sometimes i am getting the following error on loading of my page  :
Uncaught Error: Load timeout for modules : codemirror/lib/codemirror     require.js:8

what could be the reason for this... how can i debug it ?

Here is my new.js file :
require(["jquery", "detect", "utils", "codemirror/lib/codemirror", "codemirror/mode/meta", "codemirror/addon/mode/loadmode", "codemirror/addon/selection/active-line"], function ($, detect, utils, CodeMirror) {

    CodeMirror.modeURL = "codemirror/mode/%N/%N";

    var cm = = CodeMirror($('#editor').get(0), {
        styleActiveLine: true,
        lineNumbers: !detect.isMobile,

    cm.on("changes", function () {
        if (cm.getValue() !== "") {
            document.getElementById("submit").disabled = false;
        } else {
            document.getElementById("submit").disabled = true;

    var mime = $("#mode option:selected").prop("id");
    $("#mode").on("change", function() {
        mime = $("#mode option:selected").prop("id");
        var mode = CodeMirror.findModeByMIME(mime).mode;
        cm.setOption("mode", mode);
        CodeMirror.autoLoadMode(cm, mode);

    $("#submit").on("click", function(e) {               // on submtting make the editor readonly

        var objectTobeSent = {}; = null;
        objectTobeSent.title = $("#name").val();
        if (objectTobeSent.title === "") {
            objectTobeSent.title = "Untitled";
        objectTobeSent.mime = mime;//console.log(objectTobeSent.mode);
        objectTobeSent.text = cm.getValue();

        var createURL = document.getElementsByClassName("invisible")[0].dataset.createUrl;

            contentType: "application/json; charset=utf-8",
            url : createURL,
            type: "POST",
            data : JSON.stringify(objectTobeSent),
            success: function(snippet, status, xhr) {
                if (xhr.status !== 201) {
                    alert("Couldn't create code snippet. Please try after sometime");
                } else if (!utils.sendMessage(snippet, objectTobeSent.title, $("#message").val())) {
                    window.location.href = snippet.URL;
            error: function(request, status, error) {

    $("#cancel").on("click", function (e) {

It looks like this require is used in the js... please help me understand the js file also.
Is data-main specific to require.js or there its a general attribute
Join our community to see this answer!
Unlock 1 Answer and 1 Comment.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 1 Comment.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros