require.config is not a function

发布于 2022-09-04 08:21:45 字数 3056 浏览 14 评论 0

用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.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

老子叫无熙 2022-09-11 08:21:45

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

npm install requirejs安装的是2.0+版本

版本升级应该就没问题了

冰雪梦之恋 2022-09-11 08:21:45

没看出问题但是为什么你的requirejs的require.config不是一个方法?
https://cdn.bootcss.com/requi...

>require
function requirejs()
>require.config
function req.config()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文