node.js

Modules

Agenda

Research

Discussion

Q & A

Overview

Objectives

Create custom modules

Install & save modules 

Manage project metadata with package.json

Require and use NPM modules

Agenda

Research

Discussion

Q & A

Overview

Overview

function addition (x, y) {
    return x + y;
}

function subtraction (x, y) {
    return x - y;
}

function multiplication (x, y) {
    return x * y;
}

function division (x, y) {
    return x / y;
}
function runOperation (operationFn) {
    var x = $("input.x").val();
    var y = $("input.y").val();
    var result = operationFn(x, y);
    appendResult(result);
}

function calculate () {
    var $op = $('select.op');
    var operation = $op.val();

    switch( operation ) {
        case 'addition':
            runOperation(addition);
            break;
        case 'subtraction':
            runOperation(subtraction);
            break;
        // cont...
    }  
}

function appendResult  (result) {
 // cont...
}

logic.js

dom.js

How would you connect these files on the frontend?

Overview

<script type="text/javascript" src="./scripts/logic.js"/>
<script type="text/javascript" src="./scripts/dom.js"/>

index.html

But, with Node...

module.exports = {
    addition: addition,
    subtraction: subtraction,
    multiplication: multiplication,
    division: division
}

logic.js

dom.js

var operations = require('./logic');

To be clear, you won't have DOM manipulation on your server

Agenda

Research

Discussion

Q & A

Overview

Research

Resources:

  • What is the problem that the node.js Module system solves?
  • What built-in function is used to import a module?
  • What built-in object is used to export a module?
  • What types of values can a module export?
  • What are 3 ways to export an object?
  • What are 3 kinds of modules?
  • Name and describe any 3 core modules.
  • How do you require npm and core modules?
  • How do you require file modules?

Agenda

Research

Discussion

Q & A

Overview

Discussion

Resources:

  • What is the problem that the node.js Module system solves?
  • What built-in function is used to import a module?
  • What built-in object is used to export a module?
  • What types of values can a module export?
  • What are 3 ways to export an object?
  • What are 3 kinds of modules?
  • Name and describe any 3 core modules.
  • How do you require npm and core modules?
  • How do you require file modules?

Agenda

Research

Discussion

Q & A

Overview

Q & A

  • What is the problem that the node.js Module system solves?

Avoiding large files

Easily share common functionality

Easily share and use libraries

Q & A

  • What built-in function is used to import a module?

require('/path/to/filename')

Q & A

  • What built-in object is used to export a module?

module.exports

Q & A

  • What types of values can a module export?

Functions

Objects

Strings

Arrays

Numbers

Booleans

Q & A

  • What are 3 ways to export an object?
module.exports = {
    add: function(a, b) {
        return a + b;
    }
}

module.exports.add = function(a, b) {
    return a + b;
}

exports.add = function(a, b) {
    return a + b;
}

Q & A

  • What are 3 kinds of modules?

Core Modules (e.g. 'http')

File Modules (e.g. '/', './', '../')

node_modules

Q & A

  • Name and describe any 3 core modules.

For a list of Core Modules:

Modules Docs

Q & A

  • How do you require npm and core modules?

require('package-name')

Q & A

  • How do you require file modules?

With absolute/relative paths:

require('./filename')

Q & A: Push!

  • How does 'require' determine which modules to load?

It first tries to find a core module,

then tries to find a local file module,

then tries to find an installed module,

else it throws an error.

Docs

Q & A: Push!

  • What specification does the node module system impliment?

Q & A: Push!

  • How do you create a module that is an entire folder?

Entrypoint in package.json

OR

an index.js / index.node file

Docs

Q & A: Push!

  • What command is used to generate a package.json
    file for your module?

npm init

Q & A: Push!

  • How do you save module dependencies to the package.json file? Why would you do this?

npm install --save module_name

Q & A: Push!

  • How do you save module dependencies to the package.json file that are specifically for development? Why would you do this? In general, what might fall under a development dependency?

npm install --save-dev module_name

Objectives

Create custom modules

Install & save modules 

Manage project metadata with package.json

Require and use NPM modules

node.js: modules

By Wes Reid

node.js: modules

  • 1,331