Modules
Research
Discussion
Q & A
Overview
Create custom modules
Install & save modules
Manage project metadata with package.json
Require and use NPM modules
Research
Discussion
Q & A
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?
<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
Research
Discussion
Q & A
Overview
Resources:
Research
Discussion
Q & A
Overview
Resources:
Research
Discussion
Q & A
Overview
Avoiding large files
Easily share common functionality
Easily share and use libraries
require('/path/to/filename')
module.exports
Functions
Objects
Strings
Arrays
Numbers
Booleans
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;
}
Core Modules (e.g. 'http')
File Modules (e.g. '/', './', '../')
node_modules
For a list of Core Modules:
require('package-name')
With absolute/relative paths:
require('./filename')
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.
What specification does the node module system impliment?
How do you create a module that is an entire folder?
What command is used to generate a package.json
file for your module?
npm init
How do you save module dependencies to the package.json file? Why would you do this?
npm install --save module_name
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
Create custom modules
Install & save modules
Manage project metadata with package.json
Require and use NPM modules