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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14279962/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 16:15:32  来源:igfitidea点击:

Require.js Error: Load timeout for modules: backbone,jquerymobile

javascriptjquerybackbone.jsrequirejs

提问by Devin Dixon

I am trying to use r.js to optimize my code but I keep running to this error:

我正在尝试使用 r.js 来优化我的代码,但我一直遇到这个错误:

Tracing dependencies for: init

跟踪依赖项:init

Error: Load timeout for modules: backbone,jquerymobile

The command I am running is this:

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

$ 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

My build.js file looks like this:

我的 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
})

And my init.js looks like this:

我的 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
});

What am I doing wrong in this build process?

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

回答by Mike Barlow - BarDev

Require.js has a Config option called waitSeconds. This may help.

Require.js 有一个名为 waitSeconds 的配置选项。这可能会有所帮助。

RequireJS waitSeconds

RequireJS 等待秒

Here's an example where waitSeconds is used:

这是使用 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);
    }
);

回答by hcpl

The Error

错误

I recently had a very similar issue with an angularJSproject using requireJS.

我最近在一个angularJS使用requireJS.

I'm using Chrome canary build (Version 34.0.1801.0 canary) but also had a stable version installed (Version 32.0.1700.77) showing the exact same issue when loading the app with Developer consoleopen:

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

Uncaught Error: Load timeout for modules

The developer console is key heresince I didn't get the error when the console wasn't open. I tried resetting all chrome settings, uninstalling any plugin, ... nothing helped so far.

开发者控制台是这里的关键,因为当控制台未打开时我没有收到错误消息。我尝试重置所有 chrome 设置,卸载任何插件,......到目前为止没有任何帮助。

The Solution

解决方案

The big pointer was a Google group discussion (see resources below) about the waitSecondsconfig option. Setting that to 0 solved my issue. I wouldn't check this in since this just sets the timeout to infinite. But as a fix during development this is just fine. Example config:

大指针是关于waitSecondsconfig 选项的 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>

Most common other causes for this error are:

此错误最常见的其他原因是:

  • errors in modules
  • wrong paths in configuration (check pathsand baseUrloption)
  • double entry in config
  • 模块错误
  • 配置中的错误路径(检查pathsbaseUrl选项)
  • 配置中的双重输入

More Resources

更多资源

Troubleshooting page from requireJS: http://requirejs.org/docs/errors.html#timeoutpoint 2, 3 and 4 can be of interest.

来自 requireJS 的故障排除页面:http://requirejs.org/docs/errors.html#timeout point 2、3 和 4 可能很有趣。

Similar SO question: Ripple - Uncaught Error: Load timeout for modules: app http://requirejs.org/docs/errors.html#timeout

类似的问题:Ripple - Uncaught Error: Load timeout for modules: app http://requirejs.org/docs/errors.html#timeout

A related Google groups discussion: https://groups.google.com/forum/#!topic/requirejs/70HQXxNylYg

相关的 Google 群组讨论:https: //groups.google.com/forum/#!topic/requirejs/ 70HQXxNylYg

回答by Aaron

In case others have this issue and still struggling with it (like I was), this problem can also arise from circular dependencies, e.g. A depends on B, and B depends on A.

如果其他人有这个问题并且仍在努力解决它(就像我一样),这个问题也可能来自循环依赖,例如 A 依赖于 B,而 B 依赖于 A。

The RequireJS docsdon't mention that circular dependencies can cause the "Load timeout" error, but I've now observed it for two different circular dependencies.

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

回答by Eduscho

Default value for waitSeconds = 7 (7 seconds)

waitSeconds 的默认值 = 7(7 秒)

If set to 0, timeout is completely disabled.

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

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

源代码:http: //requirejs.org/docs/api.html

回答by Lyudmyla

The reason for the issue is that Require.jsruns into the timeout since the project might have dependencies to large libraries. The default timeout is 7 seconds. Increasing the value for this config option (called waitSeconds) solves it of course but it is not the right approach. Correct approach would be to improve the page loading time. One of the best technics to speed up a page loading is minification- the process of compressing the code. There are some good tools for minification like r.jsor webpack.

问题的原因是Require.js遇到超时,因为项目可能依赖于大型库。默认超时为 7 秒。增加这个配置选项(称为waitSeconds)的值当然可以解决它,但这不是正确的方法。正确的做法是改善页面加载时间。加速页面加载的最佳技术之一是缩小- 压缩代码的过程。有一些很好的缩小工具,如r.jswebpack

回答by Adam Spence

I only get this error when running tests on Mobile Safari 6.0.0 (iOS 6.1.4). waitSeconds: 0has given me a successful build for now. I'll update if my build fails on this again

我只在 Mobile Safari 6.0.0 (iOS 6.1.4) 上运行测试时遇到此错误。waitSeconds: 0现在给了我一个成功的构建。如果我的构建再次失败,我会更新