node web development 2nd edition: chapter3 node modules
DESCRIPTION
Node module concept and npm commandsTRANSCRIPT
Node ModulesRick Chang
Modules
One module per .js file
Objects not assigned to exports are not visible to any code outside the module
The globals in a module are actually local to that module
Called module encapsulation
Module
simple.js using require()var count = 0;exports.next = function() { return count++;}!exports.hello = function() { return "Hello, World!";}
$ node> var s = require('./simple');undefined> s.next()0> s.next()1> s.next()2> s.hello()'Hello, World!'
Module encapsulation
module1.js module2.js
24 Jul 15:41:32 - A = a different value A, B = a different value B, values = { A: 'value A', B: 'value B' }
var util = require("util");var m1 = require("./module1");!var A = "a different value A";var B = "a different value B";!util.log('A = ' + A + ', B = ' + B + ', values = ' + util.inspect(m1.values()));
var A = "value A";var B = "value B";exports.values = function() { return { A : A, B : B };};
Result
Module identifiers
Module identifiers: relative, absolute, top-level
relative
./, ../,
absolute
begin with /
top-level
node_modules directory
node_modules Hierarchy
Node searches the node_modules in the current directory
If not found, move to the parent directory until it reaches the root of the file system.
System-wide modules
node_modules Hierarchy
/home/david/projects/drawapp/lib/node_modules
/home/david/projects/drawapp/node_modules
/home/david/projects/node_modules
/home/david/node_modules
/home/node_modules
/node_modules
System-wide modules
All system-wide modules in /${NODE_HOME}/lib/node_modules
Package format
package.json is followed by CommonJS Pacakge/1.0 specification
The documentation of package.json
npm help json
Dependency
Package format
“dependencies": { "foo" : "1.0.0 – 2.x.x", "bar" : ">=1.0.2 <2.1.2"}
Executable commandbin: { 'nodeload.js': './nodeload.js', 'nl.js': './nl.js'}
Package formatDescribe directory structure
directories: { lib: './lib', bin: './bin' }
"scripts": { "prepublish": "npm prune", "test": "mocha --require test/support/env --reporter dot --check-leaks test/ test/acceptance/"}
Run scripts in the life cycle of the package,
which include install, activate, uninstall, update
The documentation
npm help scripts
NPM
Search package
npm search [packagename]
Public package repository
https://www.npmjs.org
NPM
Show the whole package.json
npm view [packagename]
Show the specific attributes of the package.json
npm view express dependencies
npm view express author
npm view express repository.url
NPM
Install package in system-wide modules
npm install -g express
Install package with the specific version in current directory
npm install [email protected]
Uninstall package
npm uninstall express
NPM
Eliminate the duplicate modules
npm deduce
List your installed packages
npm list
npm list -g
NPM
Go the folder of the installed package
npm explore express
npm explore express -g
Build your package
Initial your package to build the skeleton
npm init
name: (tmod2) version: (0.0.0) 0.0.1description: this is test moduleentry point: (index.js) test command: git repository: keywords: test, learnauthor: Rick Changlicense: (ISC) MITAbout to write to xxx/ch3/tmod2/package.json:!{ "name": "tmod2", "version": "0.0.1", "description": "this is test module", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "keywords": [ "test", "learn" ], "author": "Rick Chang", "license": "MIT"}
Build your package
Before publishing your package, test it
npm link
Create a symbolic link in your system-wide modules
Publish
npm publish
Configuration settings
All configuration settings are in ${HOME}/.npmrc or <Node Install Directory>/etc/npmrc
Get the config value
npm get [key]
npm config get [key]
Change the config vale
npm set [key value]
npm config set [key value]
Configuration settings
List your configuration settings
npm config list
Delete the key
npm config delete [key]
Configuration settings
Enable color mode to show npm data
npm set color true
Enable parse mode to show npm data
npm set parseable true
Enable global mode to install packages in system-wide
npm set global true