javascript - Where are the CommonJS modules? -


from time time hear commonjs http://www.commonjs.org/ effort create set of modular javascript components frankly have never understood of it.

where these modular components can use? don't see on homepage.

commonjs standard specifies way modularize javascript, commonjs not provide javascript libraries.

commonjs specifies require() function lets 1 import modules , use them, modules have special global variable named exports object holds things exported.

// foo.js ---------------- example foo module function foo() {     this.bla = function() {         console.log('hello world');     } }  exports.foo = foo;  // myawesomeprogram.js ---------------------- var foo = require('./foo'); // './' require module relative                             // in case foo.js in same directory .js file var test = new foo.foo(); test.bla(); // logs 'hello world' 

the node.js standard library , 3rd party libraries use commonjs modularize code.

one more example:

// require http module standard library var http = require('http'); // no './' require paths find module var express = require('express'); // require express.js framework (needs installed) 

Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -