How do I use it?
# Installing a package
$ npm install bluebird
# Save installed package to package.json
$ npm install bluebird --save
# Save installed package to development package list in package.json
$ npm install karma --save-dev
# Install packages in clean repository from package.json
$ npm install
How does it work?
Require desired module using CommonJS or ES6 module loading. (Will discuss modules more in the next slides.)
// CommonJS style
var Promise = require('bluebird');
// ES6 module loading
import * as Promise from 'bluebird';
// or
// has to have a default export
import Promise from 'bluebird';
// or
// has to export an object with the mapped properties
import {Foo, Bar} from 'somelib';
How does it work? (continued)
Can I install globally?
NPM allows you to install a package globally.
npm install bluebird -g
Node.js will look in the global package directory (defined in your require.paths array) for a required package if it cannot be found in the locations mentioned in the previous slide.
Can I create a command line tool?
Yes, you can configure a script to be run as a command line tool.
http://blog.npmjs.org/post/118810260230/building-a-simple-command-line-tool-with-npm
# typescript (and many other tools) works this way
$ npm install typescript -g
$ tsc ...
What does the package.json file look like?
{
"name": "bluebird",
"description": "Full featured Promises/A+ implementation with exceptionally good performance",
"version": "2.9.34",
"keywords": [
// keywords for searching on npmjs.org
],
"scripts": {
"lint": "node scripts/jshint.js",
"test": "node tools/test.js",
"istanbul": "istanbul",
"prepublish": "node tools/build.js --no-debug --main --zalgo --browser --minify"
},
"homepage": "https://github.com/petkaantonov/bluebird",
"repository": {
"type": "git",
"url": "git://github.com/petkaantonov/bluebird.git"
},
"bugs": {
"url": "http://github.com/petkaantonov/bluebird/issues"
},
"license": "MIT",
"author": {
"name": "Petka Antonov",
"email": "petka_antonov@hotmail.com",
"url": "http://github.com/petkaantonov/"
},
"devDependencies": {
"acorn": "~0.6.0",
"baconjs": "^0.7.43",
// truncated...
},
"main": "./js/main/bluebird.js",
"browser": "./js/browser/bluebird.js",
"files": [
"js/browser",
"js/main",
"js/zalgo",
"zalgo.js"
],
"maintainers": [
{
"name": "esailija",
"email": "petka_antonov@hotmail.com"
}
],
"readme": "ERROR: No README data found!"
}
Can it do anything else?
Bower works by fetching and installing packages from all over, taking care of hunting, finding, downloading, and saving the stuff you're looking for. Bower keeps track of these packages in a manifest file, bower.json. How you use packages is up to you. Bower provides hooks to facilitate using packages in your tools and workflows.
Bower is optimized for the front-end. Bower uses a flat dependency tree, requiring only one version for each package, reducing page load to a minimum.
How do I use it?
# Unlike npm, it doesn't come packaged with node.js. You must install it, via npm!
$ npm install -g bower
#######################
# Installing packages #
#######################
# registered package
$ bower install jquery
# GitHub shorthand
$ bower install desandro/masonry
# Git endpoint
$ bower install git://github.com/user/package.git
# URL
$ bower install http://example.com/script.js
How does it work?
Unlike npm, which uses CommonJS modules, you do not reference Bower modules in the same way.
Bower is focused mainly on browser modules. These are typically files that you directly include via script tags like illustrated below. Some will support AMD modules, which will be discussed in a few slides.
<script src="bower_components/jquery/dist/jquery.min.js"></script>
How does it work? (continued)
# Install bower packages configured in the bower.json file
$ bower install
What does the bower.json file look like?
{
"name": "blue-leaf",
"description": "Physics-like animations for pretty particles",
"main": [
"js/motion.js",
"sass/motion.scss"
],
"dependencies": {
"get-size": "~1.2.2",
"eventEmitter": "~4.2.11"
},
"devDependencies": {
"qunit": "~1.16.0"
},
"moduleType": [
"amd",
"globals",
"node"
],
"keywords": [
"motion",
"physics",
"particles"
],
"authors": [
"Betty Beta <bbeta@example.com>"
],
"license": "MIT",
"ignore": [
"**/.*",
"node_modules",
"bower_components",
"test",
"tests"
]
}
How do I set it up?
# Install jspm CLI
$ npm install jspm -g
# Use in a project
$ npm install jspm --save-dev
$ jspm init
# You will need to follow the instructions: Here is an example output
#
# Package.json file does not exist, create it? [yes]:
# Would you like jspm to prefix the jspm package.json properties under jspm? [yes]:
# Enter server baseURL (public folder path) [.]:
# Enter jspm packages folder [./jspm_packages]:
# Enter config file path [./config.js]:
# Configuration file config.js doesn't exist, create it? [yes]:
# Enter client baseURL (public folder URL) [/]:
# Which ES6 transpiler would you like to use, Traceur or Babel? [traceur]:
How do I use it?
# Install any packages from the jspm Registery, Github, or npm
$ jspm install npm:lodash-node
$ jspm install github:components/jquery
$ jspm install jquery
$ jspm install myname=npm:underscore
JSPM is also allows plugins, so you can install from bower for example.
# Install jspm-bower-endpoint via npm
$ npm install -g jspm-bower-endpoint
# add registry endpoint
$ jspm registry create bower jspm-bower-registry
# Install bower dependency
$ jspm install bower:skeleton
How does it work?
This gets more into module loading and I will cover this in that section while also talking about RequireJS, Browserify, WebPack, etc.
Both:
Examples
// CommonJS Example
var $ = require('jquery');
exports.nameElement = $('input.name');
// AMD Example
define(['jquery'], function ($) {
return {
nameElement: $('input.name')
};
});
CommonJS
// Export multiple properties as example.js
var $ = require('jquery');
var _ = require('lodash');
var fruit = ['Apple', 'Banana', 'Peach'];
exports.nameElement = $('input.name');
exports.findFruit = function (predicate) {
return _.where(fruit, predicate);
};
// Consume example.js
var example = require('./example');
var nameElement = example.nameElement;
var fruitWithFiveLetters = example.findFruit(function (fruit) {
return fruit.length === 5;
});
CommonJS (weirdness)
// Export function (class) person.js
function Person (name, age) {
this.name = name;
this.age = age;
}
exports.Person = Person;
// Consume person.js
var Person = require('./person');
var adam = new Person.Person('Adam Carr', 34); // I hate redundancy!!
/************
* The fix! *
***********/
// person.js
function Person (name, age) {
this.name = name;
this.age = age;
}
exports = Person;
// Consume person.js
var Person = require('./person');
var adam = new Person('Adam Carr', 34);
CommonJS (ES6)
// person.js
export function canDrink (person) {
return person.age >= 21;
}
export default function Person (name, age) {
this.name = name;
this.age = age;
}
// Consume person.js
import Person from './person';
var adam = new Person('Adam Carr', 34);
// How do I get to canDrink? You can't this way.
// Consume person.js
import Person, { canDrink } from './person';
var adam = ...;
canDrink(adam); // true
// What if there were more exported properties, how do I get them all?
// Do I have to list each on? You can, and there are advantages to listing
// them all (performance wise), but you can also import all to a local object
import Person, * as PersonExtra from './person';
var adam = ...;
PersonExtra.canDrink(adam); // true
PersonExtra.isHandsome(adam); // we don't know, it is not implemented
// NOTE: Person could also be referenced as follows in this example
var john = new PersonExtra.default('John Doe', 100);
AMD
// Export multiple properties as example.js
define(['jquery', 'lodash'], function ($, _) {
var fruit = ['Apple', 'Banana', 'Peach'];
return {
namedElement: $('input.name'),
findFruit: function (predicate) {
return _.where(fruit, predicate);
}
};
});
// Consume example.js
define(['./example'], function (example) {
var nameElement = example.nameElement;
var fruitWithFiveLetters = example.findFruit(function (fruit) {
return fruit.length === 5;
});
});
AMD (no weirdness)
// Export function (class) person.js
// Since it is a usual require statement, you naturally would just return
// the function vs assigning to exports. Easier to grok for me at least.
define([], function () {
function Person (name, age) {
this.name = name;
this.age = age;
}
return Person;
});
// Consume person.js
define(['./person'], function (Person) {
var adam = new Person('Adam Carr', 34);
});
In the browser
RequireJS
browserify
JSPM
WebPack
TypeScript
TypeScript lets you write JavaScript the way you really want to. TypeScript is a typed superset of JavaScript that compiles to plain JavaScript. Any browser. Any host. Any OS, Open Source
TypeScript (Example)
class Greeter {
constructor (public message: string) { }
greet() {
return `Hello, ${this.message}`;
}
}
var greeter = new Greeter("world");
var button = document.createElement('button')
button.innerText = "Say Hello"
button.onclick = function() {
alert(greeter.greet())
}
document.body.appendChild(button)
var Greeter = (function () {
function Greeter(message) {
this.message = message;
}
Greeter.prototype.greet = function () {
return "Hello, " + this.message;
};
return Greeter;
})();
var greeter = new Greeter("world");
var button = document.createElement('button');
button.innerText = "Say Hello";
button.onclick = function () {
alert(greeter.greet());
};
document.body.appendChild(button);
CoffeeScript
CoffeeScript is a little language that compiles into JavaScript. Underneath that awkward Java-esque patina, JavaScript has always had a gorgeous heart. CoffeeScript is an attempt to expose the good parts of JavaScript in a simple way.
CoffeeScript (Example)
class Greeter
constructor: (@message) ->
greet: () ->
return "Hello, #{@message}"
greeter = new Greeter 'World'
button = document.createElement 'button'
button.innerText = 'Say Hello'
button.onclick ->
alert greeter.greet()
document.body.appendChild button
var Greeter, button, greeter;
Greeter = (function() {
function Greeter(message) {
this.message = message;
}
Greeter.prototype.greet = function() {
return "Hello, " + this.message;
};
return Greeter;
})();
greeter = new Greeter('World');
button = document.createElement('button');
button.innerText = 'Say Hello';
button.onclick(function() {
return alert(greeter.greet());
});
document.body.appendChild(button);
Common features: