Your browser doesn't support the features required by impress.js, so you are presented with a simplified version of this presentation.
For the best experience please use the latest Chrome, Safari or Firefox browser.
Own NPM modules - pitfalls.
Writing custom re-usable NPM modules and how not to mess it up.
Sydney, 10th June 2014
Nik Butenko
{
"name": "shared",
"repository": {
"type": "git",
"url": "https://github.com/nkbt/nnj-shared.git"
}
}
var config = {
sayYes: false
};
function say() {
if (config.sayYes) {
return "Yes!";
}
return "No.";
}
function sayYes(shouldSayYes) {
config.sayYes = !!shouldSayYes;
}
exports.sayYes = sayYes;
exports.say = say;
{
"name": "nnj-lib",
"dependencies": {
"nnj-shared": "git://github.com/nkbt/nnj-shared.git"
},
"repository": {
"type": "git",
"url": "https://github.com/nkbt/nnj-lib.git"
}
}
var sharedLib = require('shared');
sharedLib.sayYes(true);
function alwaysYes() {
return sharedLib.say();
}
exports.alwaysYes = alwaysYes;
{
"name": "nnj",
"dependencies": {
"nnj-lib": "git://github.com/nkbt/nnj-lib.git",
"nnj-shared": "git://github.com/nkbt/nnj-shared.git"
},
"repository": {
"type": "git",
"url": "https://github.com/nkbt/nnj.git"
}
}
var shared = require('shared');
shared.sayYes(false);
function alwaysNo() {
return shared.say();
}
console.log('alwaysNo() should say "No." ', alwaysNo());
var lib = require('lib');
console.log('lib.alwaysYes() should say "Yes!" ', lib.alwaysYes());
console.log('alwaysNo() should say "No." ', alwaysNo());
> node app
alwaysNo() should say "No." No.
lib.alwaysYes() should say "Yes!" Yes!
alwaysNo() should say "No." No.
> node app
alwaysNo() should say "No." No.
lib.alwaysYes() should say "Yes!" Yes!
alwaysNo() should say "No." Yes!
app.js
package.json
node_modules
lib
index.js
package.json
shared
index.js
package.json
app.js
package.json
node_modules
lib
index.js
package.json
node_modules
shared
index.js
package.json shared
index.js
package.json
module.exports = function(sayYes) {
return {
say: function() {
if (sayYes) {
return "Yes!";
}
return "No.";
}
}
};
// Usage in app.js
var shared = require('shared')(true);
console.log(shared.say()); // Will say: "Yes!"
function Shared(sayYes) {
this._sayYes = sayYes;
}
Shared.prototype.say = function () {
if (this._sayYes) {
return "Yes!";
}
return "No.";
};
module.exports = Shared;
// Usage in app.js
var Shared = require('shared');
var shared = new Shared(true);
console.log(shared.say()); // Will say: "Yes!"
{
"name": "lib",
"dependencies": {
"nnj-shared": "git://github.com/nkbt/nnj-shared.git"
},
"bundledDependencies": [
"nnj-shared"
],
"repository": {
"type": "git",
"url": "https://github.com/nkbt/nnj-lib.git"
}
}