Is 'require(…)' a common javascript pattern or a library function?

后端 未结 3 971
小鲜肉
小鲜肉 2020-11-29 08:43

I usually find this as the first line in node.js scripts/modules as well as phantomJS, casperJS etc. I\'m curious, if this is a common pattern for server-side javascript (SS

相关标签:
3条回答
  • 2020-11-29 09:12

    The others gave nice technical answers, but I wanted to provide a super simplified answer just in case it helps someone in the future. "require" isn't in itself a library, but it's used when you want access modules (libraries and frameworks and such). So it's not like "require" is the most popular library or anything like that :).

    By the way, plenty of older code demands that you still use "require" (notice I avoided saying it's required), but new syntax has been developed (I guess with ES6, from 2015) using the word "import." I definitely prefer import, and it is used for the exact same purpose, though it does look a bit different.

    Anyways, as the others mentioned, "require" means you are requiring (i.e. accessing) a module. But that doesn't necessarily mean that you are getting access to a library or framework... you might actually be getting access to another page that you yourself created! For example, you might see this:

     - var Comment = require("./models/comment");
    

    This just means "give me access to that comment file I made inside the models directory." This is also considered a module.

    So you can think of it like this... you require some code (or you import it)... so that you can utilize it the way you want. If you didn't require/import it, you wouldn't get it.

    0 讨论(0)
  • 2020-11-29 09:26

    The require() idiom is part of a specification known as CommonJS. Specifically, that part of the spec is called 'Modules'. RequireJS is just one implementation of CommonJS (and it's usually a browser-side implementation - in fact, it takes a different approach because of the asynchronous nature of the browser).

    If you look at the list of implementations on the CommonJS site, you'll see Node.js listed. Notice that it implements 'Modules'. Hence, that's where it's coming from - it's very much built-in.

    0 讨论(0)
  • 2020-11-29 09:34

    The require in PhantomJS and Node.js means exactly the same with the difference that none of the base modules match. Although the fs module exists for both, they are different and do not provide the same functions.

    require is functionally the same in PhantomJS and Node.js. CasperJS is built on top of PhantomJS and uses its require function, but also patches it. With CasperJS it is also possible to require a module with its name such as require('module') instead of require('./module') if it is in the same directory.

    Full matrix (file.js is in the same directory as the executed script):

                | node
                |   | phantom
                |   |   | casper
                |   |   |   | slimer
    ------------+---+---+---+--------
    file        | n | n | y | y
    ./file      | y | y | y | y
    file.js     | n | n | n | n
    ./file.js   | n | n | n | n
    

    PhantomJS can also use modules defined in the special folder node_modules in the same way that node does. It cannot use actual node modules that have dependencies to modules that are not present in PhantomJS.

    Examples of what can be required:

    m.js (for functions)

    module.exports = function(){
        return {
            someKey: [1,2,3,4],
            anotherKey: function(){
                console.log("module exports works");
            }
        }
    };
    

    e.js (for everything else as JS)

    exports.someKey = {
        innerKey: [1,2,3,4]
    };
    
    exports.anotherKey = function(){
        console.log("exports works");
    };
    

    a.json (arbitrary JSON)

    [
        {
            "someKey": [ 1,2,3,4 ],
            "anotherKey": 3
        }
    ]
    

    script.js

    var m = require("./m")();
    m.anotherKey(); // prints "module exports works"
    
    var e = require("./e");
    e.anotherKey(); // prints "exports works"
    
    var a = require("./a");
    console.log(a[0].anotherKey); // prints "3"
    
    0 讨论(0)
提交回复
热议问题