NPM

In notebook:
FrontEndMasters Building Web Apps with Node.js
Created at:
2015-10-04
Updated:
2015-10-08
Tags:
backend Node JS JavaScript
https://frontendmasters.com/courses/building-web-apps-with-node-js/#v=a22b1944uc&skip=1
  • node's package manager (ruby gems)
  • handles and resolves dependencies (Bundler)
  • 60,000 + modules
​$npm install twilio​ 

will install to
./node_modules

./node_modules/twilio:

  • package.json
  • index.js (entry point for the module)

package.json

  • name properties, info about the project
  • manage dependencies (​"dependencies:{...}"​)
  • ​"devDependencies"​ - only in a development environment
  • ​"scripts"​ declare tasks (​"scripts":{"test":"jasmine-node spec"}​ -> ​$ node test​ will run the jasmine tests (like Grunt)
  • ​"main"​ the path to the javascript file which is the main part of the library (e.g. /lib/index.js) : in /lib you can have an index.js which defines the public interfaces
  • ​"engines"​ which version of node to use

then you can use it
  var twilio = require("twilio");

var client = twilio();

client.sendMessage({
    to: "+1555555",
    from: "+15555555",
    body: "hello wold"
});
most modules use an async API (here in twilio a callback)

error-first
  client.sendMessage({},
    function(err,message){
        // advises to use this pattern 
        // when writing modules for NodeJS
    }
)