Javascript Notebook
  • Introduction
  • Intro to JavaScript
    • Section 1 - Getting Started
    • Section 2 - Execution Contexts and Lexical Environements
    • Section 3 - Types and Operators
    • Section 3 Part 2 - Closures and Callbacks
    • Section 4 - Objects and Functions
    • Section 5 - Object Oriented Javascript and Prototypal Inheritance
    • Section 6 - Building Objects
    • Section 7 - Odds and Ends
    • Section 8 - Examining Famous Frameworks and Libraries
    • Section 9 - Let's Build a Framework or Library!
  • Midterm Review
  • Final Review
  • jQuery
    • Section 1 - Selectors
    • Section 2 - Events
    • Section 3 - Effects
  • Node.js
    • The Node Core
    • Modules, Exports, and Require
    • Events and the Event Emitter
    • Databases and SQl
  • D3.js
    • Diving In
    • Bar Chart
    • Creating A Complex Bar Chart
Powered by GitBook
On this page
  • Modules:
  • How does it really work?
  • Require more than one object
  • Incorporating JSON with Require
  • Module Patterns
  • Pattern 3
  • Require Core Native Modules

Was this helpful?

  1. Node.js

Modules, Exports, and Require

Modules:

  • Reusable block of code whose existence doesn't accidentally impact other code.

  • Code that doesnt affect the rest of the node framework

greet.js

var greet = function() {
    console.log('hello');
};

greet();

app.js

require('./greet.js')

commandline

node app.js // hello
  • Tells where to look

  • Allows us to individualize files or modules

  • Notice that this doesnt allow us to use any of the functionalities in app.js (its not an #include).

    • This is by design by module, the file is protected and self contained.

    • But there is a way to make it available

greet.js

var greet = function() {
    console.log('hello');
};

module.exports = greet;

app.js

var greet = require('./greet.js')

greet();

commandline

node app.js // hello

How does it really work?

  • Modules use the advantages in variable scope within functions / immediately invoked functions.

  • Require:

    • Wraps the code you write within a function expression.

    • Everything you write in a node module is wrapped within a function expression

    • Require returns the module that you write.

      • Within your own module, if you've exported anything, you basically are changing back your own module that eventually becomes returned.

Require more than one object

  • Build a new folder than the require that folder, which can contain multiple contents.

app2.js

var greet = function() {
    console.log('hello');
};

module.exports = greet;

app3.js

var greet = function() {
    console.log('hola');
};

module.exports = greet;

index.js

var english = require("./app2.js");
var spanish = require("./app3.js");

module.exports = {
    english: english,
    spanish: spanish
};

app.js

var greet = require('./multiple')

greet.english();
greet.spanish();

commandline

node app.js
// hello
// hola

Incorporating JSON with Require

app2.js

var greetings = require("./data.json")

var greet = function() {
    console.log(greetings.en);
};

module.exports = greet;

app3.js

var greetings = require("./data.json")

var greet = function() {
    console.log(greetings.es);
};

module.exports = greet;
{
    "en": "Hello",
    "es": "Hola"
}

index.js

var english = require("./app2.js");
var spanish = require("./app3.js");

module.exports = {
    english: english,
    spanish: spanish
};

app.js

var greet = require('./multiple')

greet.english();
greet.spanish();

commandline

node app.js
// hello
// hola

Module Patterns

Pattern 1

  • We can also do something like

module.exports = function(){
  console.log("hello world");
}
module.exports.greet = function(){
  console.log("hello world!!");
}
var greet = require("./multiple/app2.js");
greet();

var greet2 = require("./multiple/app3.js").greet;
greet2();
hello world
hello world!!
  • Here we're essentially building off the require object that is returned.

Pattern 2

app4.js

function Greet() {
    this.greeter = "Hello world!!!!";
    this.greet = function () {
        console.log(this.greeter);
    }
}

module.exports = new Greet();

app.js

var greet3 = require("./multiple/app4.js");
greet3.greet(); // Hello world!!!! 

greet3.greeter = "Changed Hello World";

var greet3b = require("./multiple/app4.js");
greet3b.greet(); // Changed Hello World
  • Notice that even though we've created a new object, the modifications made to greet3 as an object, affected all subsequent call for newly created objects. The all essentially become the same object.

    • This is because the changes are cached within the require function.

To Avoid This

  • Simply return the function constructor entirely and avoid creating new objects there

app4.js

function Greet() {
    this.greeter = "Hello world!!!!";
    this.greet = function () {
        console.log(this.greeter);
    }
}

module.exports = Greet;
var greet3 = require("./multiple/app4.js");
var test = new greet3();
test.greet(); // Hello world!!!! 
test.greeter = "Changed Hello World";

var greet3b = require("./multiple/app4.js");
var test2 = new greet3b();
test2.greet(); // Hello world!!!!

Pattern 3

app4.js

var greeting = 'hello'

function greet() {
    console.log(greeting);
}

module.exports = {
    greet:greet
}

app.js

var foo = require("./multiple/app4.js");

foo.greet(); // hello
  • Why this is useful:

    • greeting is encapsulating within app4.js, and the only thing we export is the greeter function.

    • This means we do not have access to greeting. The variable essentially becomes private to the module - which is good because it wouldn't be a good idea to change it within the application.

Require Core Native Modules

  • There are already modules that are built within node that are native to the functionality of node.js itself. AKA node api.

    • Some are global

    • Others require 'includes'

  • All of this can be found within the api documentation for node.js

var util = require("util"); // including lib

var name = 'Dan'
var greeting = util.format("Hello, %s", name); // C style
util.log(greeting); // 14 May 16:00:52 - Hello, Dan
PreviousThe Node CoreNextEvents and the Event Emitter

Last updated 5 years ago

Was this helpful?