Require.js错误:加载模块的超时:backbone,jquerymobile

Dev*_*xon 60 javascript jquery requirejs backbone.js

我试图使用r.js来优化我的代码,但我一直在运行这个错误:

跟踪以下内容的依赖关系:init

Error: Load timeout for modules: backbone,jquerymobile
Run Code Online (Sandbox Code Playgroud)

我正在运行的命令是这样的:

$ java -classpath /Users/dixond/build-tools/rhino1_7R4/js.jar:/Users/dixond/build-tools/closurecompiler/compiler.jar org.mozilla.javascript.tools.shell.Main /Users/dixond/build-tools/r.js/dist/r.js -o /Users/dixond/Sites/omm_mobile/js/build.js
Run Code Online (Sandbox Code Playgroud)

我的build.js文件如下所示:

( {
    //appDir: "some/path/",
    baseUrl : ".",
    mainConfigFile : 'init.js',
    paths : {
        jquery : 'libs/jquery-1.8.3.min',
        backbone : 'libs/backbone.0.9.9',
        underscore : 'libs/underscore-1.4.3',
        json2 : 'libs/json2',
        jquerymobile : 'libs/jquery.mobile-1.2.0.min'
    },
    packages : [],
    shim : {
        jquery : {
            exports : 'jQuery'
        },
        jquerymobile : {
            deps : ['jquery'],
            exports : 'jQuery.mobile'
        },
        underscore : {
            exports : '_'
        },
        backbone : {
            deps : ['jquerymobile', 'jquery', 'underscore'],
            exports : 'Backbone'
        }
    },
    keepBuildDir : true,
    locale : "en-us",
    optimize : "closure",
    skipDirOptimize : false,
    generateSourceMaps : false,
    normalizeDirDefines : "skip",
    uglify : {
        toplevel : true,
        ascii_only : true,
        beautify : true,
        max_line_length : 1000,
        defines : {
            DEBUG : ['name', 'false']
        },


        no_mangle : true
    },
    uglify2 : {},
    closure : {
        CompilerOptions : {},
        CompilationLevel : 'SIMPLE_OPTIMIZATIONS',
        loggingLevel : 'WARNING'
    },
    cssImportIgnore : null,
    inlineText : true,
    useStrict : false,
    pragmas : {
        fooExclude : true
    },
    pragmasOnSave : {
        //Just an example
        excludeCoffeeScript : true
    },
    has : {
        'function-bind' : true,
        'string-trim' : false
    },
    hasOnSave : {
        'function-bind' : true,
        'string-trim' : false
    },
    //namespace: 'foo',
    skipPragmas : false,
    skipModuleInsertion : false,
    optimizeAllPluginResources : false,
    findNestedDependencies : false,
    removeCombined : false,
    name : "init",
    out : "main-built.js",
    wrap : {
        start : "(function() {",
        end : "}());"
    },
    preserveLicenseComments : true,
    logLevel : 0,
    cjsTranslate : true,
    useSourceUrl : true
})
Run Code Online (Sandbox Code Playgroud)

我的init.js看起来像这样:

 requirejs.config({
      //libraries
      paths: {
          jquery:       'libs/jquery-1.8.3.min',
          backbone:     'libs/backbone.0.9.9',
          underscore:   'libs/underscore-1.4.3',
          json2 :       'libs/json2',
          jquerymobile: 'libs/jquery.mobile-1.2.0.min'
      },

      //shimming enables loading non-AMD modules
      //define dependencies and an export object
      shim: {
          jquerymobile: {
              deps: ['jquery'],
              exports: 'jQuery.mobile'
          },
          underscore: {
              exports: '_'
          },
          backbone: {
              deps: ['jquerymobile', 'jquery', 'underscore', 'json2'],
              exports: 'Backbone'
          }
      }
    });


requirejs(["backbone",], function(Backbone) {
    //Execute code here
});
Run Code Online (Sandbox Code Playgroud)

我在这个构建过程中做错了什么?

Mik*_*Dev 106

Require.js有一个名为waitSeconds的Config选项.这可能有所帮助.

RequireJS waitSeconds

以下是使用waitSeconds的示例:

requirejs.config({
    baseUrl: "scripts",
    enforceDefine: true,
    urlArgs: "bust=" + (new Date()).getTime(),
    waitSeconds: 200,
    paths: {
        "jquery": "libs/jquery-1.8.3",
        "underscore": "libs/underscore",
        "backbone": "libs/backbone"
    },
    shim: {
        "underscore": {
            deps: [],
            exports: "_"
        },
        "backbone": {
            deps: ["jquery", "underscore"],
            exports: "Backbone"
        },
    }
});

define(["jquery", "underscore", "backbone"],
    function ($, _, Backbone) {
        console.log("Test output");
        console.log("$: " + typeof $);
        console.log("_: " + typeof _);
        console.log("Backbone: " + typeof Backbone);
    }
);
Run Code Online (Sandbox Code Playgroud)

  • 谢谢!这对我有帮助.我想知道,大多数人都使用此属性的非默认值吗?200很常见吗?0怎么样? (3认同)
  • 在清除缓存后第一次加载时,我在iOS/Safari(而不是iOS/Chrome!)中也会出现超时.骨干视图无法加载超时.设置waitSeconds真的很高没有帮助.我的require()调用包括8个模块,每个模块包括2到3个自己的模块(一些是text!templates).如果我将8个模块减少到5个,那么在iOS/Safari下可以正常运行.我已经检查了文件路径以及语法错误十几次. (2认同)

hcp*_*cpl 42

错误

我最近使用的angularJS项目有一个非常类似的问题requireJS.

我正在使用Chrome canary build(Version 34.0.1801.0 canary)但安装了一个稳定版本(Version 32.0.1700.77),在使用Developer consoleopen 加载应用时显示完全相同的问题:

Uncaught Error: Load timeout for modules
Run Code Online (Sandbox Code Playgroud)

开发人员控制台是关键,因为我没有在控制台未打开时收到错误.我尝试重置所有Chrome设置,卸载任何插件,...到目前为止没有任何帮助.

解决方案

最重要的指针是关于waitSeconds配置选项的Google小组讨论(请参阅下面的资源).将其设置为0解决了我的问题.我不会检查这个,因为这只是将超时设置为无限.但作为开发过程中的修复,这很好.示例配置:

<script src="scripts/require.js"></script>
<script>
  require.config({
    baseUrl: "/another/path",
    paths: {
      "some": "some/v1.0"
    },
    waitSeconds: 0
  });
  require( ["some/module", "my/module", "a.js", "b.js"],
    function(someModule,    myModule) {
      //This function will be called when all the dependencies
      //listed above are loaded. Note that this function could
      //be called before the page is loaded.
      //This callback is optional.
    }
  );
</script>
Run Code Online (Sandbox Code Playgroud)

导致此错误的最常见原因是:

  • 模块中的错误
  • 配置中的错误路径(检查pathsbaseUrl选项)
  • 配置中的双重输入

更多资源

来自requireJS的疑难解答页面:http: //requirejs.org/docs/errors.html#timeout第2,3和4点可能很有用.

类似的问题:波纹 - 未捕获错误:模块加载超时:app http://requirejs.org/docs/errors.html#timeout

相关的Google论坛讨论:https://groups.google.com/forum/#!topic / simplyjs/70HQXxNylYg


Aar*_*ron 18

如果其他人有这个问题但仍然在努力(就像我一样),这个问题也可能来自循环依赖,例如A取决于B,B取决于A.

RequireJS文档没有提到循环依赖可能导致"加载超时"的错误,但现在我已经观察到这两个不同的循环依赖.

  • 你是如何确定循环问题的?您是否必须浏览每个定义的模块? (2认同)
  • [xrayquire](https://github.com/requirejs/xrayquire)(requirejs的工具)可以检查循环依赖 (2认同)

Edu*_*cho 17

waitSeconds的默认值= 7(7秒)

如果设置为0,则完全禁用超时.

src:http://requirejs.org/docs/api.html