RequireJS not loading a file or module named “module.js”

前端 未结 2 1006
温柔的废话
温柔的废话 2021-01-24 17:23

I just started using RequireJS. I tried a simple code but one way works but the other doesn\'t.

folder \"script\" has \"main.js\", \"module.js\", \"require.js\"

相关标签:
2条回答
  • 2021-01-24 17:58

    Use a different name than module for your module. For one thing, it is a terribly uninformative name, but the module named module is a special module for RequireJS. It is a module that gives information about the module you are currently in. For instance if foo.js contains this code:

    define(['module'], function (module) {
        console.log(module.id);
    });
    

    and this file is loaded when you request the module named foo, then console.log will show "foo" on the console.

    The documentation does not highlight the existence of module but it talks about it when explaining what the configuration option config does. Because you get to access the config of your module through module.config().

    The reason requiring "script/module.js" works is because when you do this you require a module named script/module.js rather than module.

    0 讨论(0)
  • 2021-01-24 18:08

    I continued reading the documentation:

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

    and it led to a github that has information about this:

    https://github.com/jrburke/requirejs/wiki/Differences-between-the-simplified-CommonJS-wrapper-and-standard-AMD-define#magic

    It turns out that "module" is a kind of "magic modules" along as "require", "export".

    And "module" ... :

    gives you information about the module ID and location of the current module

    https://github.com/jrburke/requirejs/wiki/Differences-between-the-simplified-CommonJS-wrapper-and-standard-AMD-define#module

    0 讨论(0)
提交回复
热议问题