node.js modules 

+

npm

Objectives

  • Describe what a Node.js module is and why they're beneficial
  • Explain what the three kinds of modules are
  • Discuss what npm is and what it's used for
  • Manage project metadata with package.json
  • Require and use npm modules

Review

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?

Review

<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

Review

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

 

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

 

  • How do you require file modules?

⬜️

require('/path/to/filename')

require( )

module.exports

Node Module Research

Resources:

  • What is the problem that the node.js Module system solves?
  • 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?

Node Module 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

  • What types of values can a module export?

Functions

Objects

Strings

Arrays

Numbers

Booleans

Node Module 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;
}

Node Module Q & A

  • What are 3 kinds of modules?

Core Modules (e.g. 'http')

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

node_modules

Node Module Q & A

  • Name and describe any 3 core modules.

For a list of Core Modules:

Modules Docs

Node Module Q & A

  • How do you require npm and core modules?

require('package-name')

Node Module Q & A

  • 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

Node Module Q & A

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

BREAK!

npm!

it doesn't stand for node package manager.

npm!

"npm" is named after its command-line utility, which was organically selected to be easily typed by a right-handed programmer using a US QWERTY keyboard layout, ending with the right pinky finger in a position to type the - key for flags and other command-line arguments.

npm Research

  • What command do you use to initialize npm in a project directory? What file does that command create?
  • Why is a package.json helpful?
  • What is a dependency? How do you save a dependency to a project's package.json?
  • How do you save module dependencies to the package.json file that are specifically for development? Why would you do this?
  • What command do you use to install dependencies from an already existing package.json?
  • What directory is created when you run npm install?

npm Q & A

What command do you use to initialize npm in a project directory? What file does that command create?

npm init

 

package.json

npm Q & A

Why is a package.json helpful?

  1. It serves as documentation for what packages your project depends on.
  2. It allows you to specify the versions of a package that your project can use using semantic versioning rules.
  3. Makes your build reproduceable which means that its way easier to share with other developers.
    • The node_modules directory is huge!

npm Q & A

What is a dependency? How do you save a dependency to a project's package.json?

It is a module your project depends on to work.

 

npm install --save <module_name>

npm Q & A

How do you save module dependencies to the package.json file that are specifically for development?

Why would you do this?

npm install --save-dev <module_name>

 

These dependencies are needed for development not for the application to run.

npm Q & A

What command do you use to install dependencies from an already existing package.json?

npm install

npm Q & A

What directory is created when you run npm install?

node_modules

Pro tip: Add node_modules/ to .gitignore immediately after npm install

node modules + npm

By Valerie Kraucunas

node modules + npm

  • 1,171