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

后端 未结 3 983
小鲜肉
小鲜肉 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: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.

提交回复
热议问题