COMMONJS Specification (RPM)

Source: Internet
Author: User

Overview

COMMONJS is a specification for server-side modules, and node. JS uses this specification.

According to the COMMONJS specification, a single file is a module. The load module uses the Require method, which reads a file and executes it, and finally returns the exports object inside the file. The following is a simple module file example.js.

Console.log ("Evaluating Example.js"); var function () {  console.log ("invisible"= "HI"function  () {  Console.log ( message);}

Using the Require method, load the example.js.

var example = require ('./example.js ');

At this point, the variable example corresponds to the exports object in the module, so it is possible to use the various methods provided by the module through this variable.

{  "hi",  say: [Function]}

The Require method reads the JS file by default, so you can omit the JS suffix name.

varexample=require(‘./example‘);

The JS file name needs to be preceded by a path, which can be a relative path (as opposed to a file using the Require method) or an absolute path. If you omit the path, node. JS will assume that you want to load a core module, or a module that is already installed in the local node_modules directory. If a directory is loaded, node. JS will first look for the Package.json file in that directory, load the module referenced by the main property of the file, or look for the Index.js file under that directory. Look at a more complicated example.

// Foobar.js function Foobar () {        thisfunction() {                console.log (' Hello foo ');        }          This function () {                console.log (' Hello bar ');         = Foobar;

The method to invoke the module is as follows:

var New  //  ' Hello bar '

Sometimes, you don't need exports to return an object, only it returns a function. At this time, we should write Module.exports.

function () {  console.log ("Hello World")}

AMD specification compatibility with the COMMONJS specification

The COMMONJS specification loading module is synchronous, that is, only the loading is complete before the subsequent operation can be performed. The AMD specification is a non-synchronous loading module that allows you to specify a callback function. Since node. JS is primarily used for server programming, the module files are generally present in the local hard disk, so it is faster to load and do not consider the way of non-synchronous loading, so the COMMONJS specification is more applicable. However, if it is a browser environment, to load the module from the server side, it must be in an asynchronous mode, so the browser side is generally used AMD specifications.

The AMD specification uses the Define method to define the module, and here is an example:

function (Lib) {     function  foo () {        lib.log (' Hello world! ') );    }       return {        foo:foo    };});

The AMD specification allows the output module to be compatible with the COMMONJS specification, at which point the Define method needs to be written as follows:

Define (function  (require, exports, module) {    var somemodule = require ("Somemodule"  );     var anothermodule = require ("Anothermodule");        Somemodule.dotehawesome ();    Anothermodule.domoarawesome ();     function () {        somemodule.dotehawesome ();        Anothermodule.domoarawesome ();    };});

Reference links

Addy Osmani, Writing Modular JavaScript with AMD, CommonJS & ES Harmony

Reprinted from: HTTP://JAVASCRIPT.RUANYIFENG.COM/NODEJS/COMMONJS.HTML#TOC0

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.