requirejs - require.config is not a function

查看:1076
本文介绍了requirejs - require.config is not a function的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

用webpack是正常,但由于某些原因,还是试了下requirejs,但结果出错了,暂时找不出原因。

main.js

require.config({
    paths: {
        "Vue": "javascripts/vue",
        "VueRouter": "javascripts/vue-router"
    }
});

require(['Vue', 'VueRouter'], function (Vue, VueRouter) {
    // 具体代码
});

页面加载代码

<script src="../javascripts/require.min.js" data-main="../main"></script>

看不出哪里错了,可是结果错了。

如果这样写会报错

<script src="../javascripts/require.min.js"></script>
<script src="../main.js"></script>

报错

Uncaught TypeError: require.config is not a function

写了个测试。

<script src="../javascripts/require.min.js"></script>
<script src="../javascripts/test.js"></script>

test.js执行正常。

test.js


define('module', [], function(dep1, dep2) {
    console.log('setup module1');
    return {
        api: function() {
            console.log('from module1');
        }
    };
});

define('module2', [], function() {
    console.log('setup module2');
    var module = require('module');
    return {
        api2: function() {
            console.log('from module2');
        }
    };
});

define('module3', ['module'], function(module) {
    console.log('setup module3');
    return {
        api3: function() {
            console.log('from module3');
        }
    };
});

define('main', ['module2', 'module'], function(module2, module) {
    module2.api2();
    module.api();
    require('module3').api3();
});

console.time('time');
require('main');
console.timeEnd('time');

<script data-main="../javascripts/test" src="../javascripts/require.min.js"></script>

不执行,暂时还没弄清楚。

可能原因

If you get that error, it could be that:

RequireJS is not loaded.

This can diagnosed by opening the browser's debugger to look at network requests and seeing whether the file that defines require is asked by the browser and checking that the response indicates that the data is available (i.e. loaded from the server, loaded from cache).
RequireJS is loaded but not initializing properly.

This could happen if something is loaded before RequireJS and somehow messes with the JavaScript runtime in a way that makes RequireJS fail to initialize. For instance, if something puts in the global space a global named define of any type, or global named requirejs and which is a function, then RequireJS will silently abandon initializing.

I'd diagnose this with breakpoints in the file that contains RequireJS to see whether it completes execution or not.
RequireJS is loaded but something else redefines require.

I'd inspect the value of require just after RequireJS is loaded. If it has a config method defined at that point, then something else must be messing with it later.

初步验证:
RequireJS初始化和加载没问题;
写了个简单的例子测试,应该没有其他引用导致RequireJS初始化失败;
RequireJS版本1.0.

可能是版本问题,升级下试试。

解决方案

npm install require.js 安装的是1.0版本

npm install requirejs安装的是2.0+版本

版本升级应该就没问题了

这篇关于requirejs - require.config is not a function的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆