21
Node Modules Rick Chang

Node Web Development 2nd Edition: Chapter3 Node Modules

Embed Size (px)

DESCRIPTION

Node module concept and npm commands

Citation preview

Page 1: Node Web Development 2nd Edition: Chapter3 Node Modules

Node ModulesRick Chang

Page 2: Node Web Development 2nd Edition: Chapter3 Node Modules

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

Page 3: Node Web Development 2nd Edition: Chapter3 Node Modules

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!'

Page 4: Node Web Development 2nd Edition: Chapter3 Node Modules

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

Page 5: Node Web Development 2nd Edition: Chapter3 Node Modules

Module identifiers

Module identifiers: relative, absolute, top-level

relative

./, ../,

absolute

begin with /

top-level

node_modules directory

Page 6: Node Web Development 2nd Edition: Chapter3 Node Modules

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

Page 7: Node Web Development 2nd Edition: Chapter3 Node 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

Page 8: Node Web Development 2nd Edition: Chapter3 Node Modules

System-wide modules

All system-wide modules in /${NODE_HOME}/lib/node_modules

Page 9: Node Web Development 2nd Edition: Chapter3 Node Modules

Package format

package.json is followed by CommonJS Pacakge/1.0 specification

The documentation of package.json

npm help json

Page 10: Node Web Development 2nd Edition: Chapter3 Node Modules

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'}

Page 11: Node Web Development 2nd Edition: Chapter3 Node Modules

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

Page 12: Node Web Development 2nd Edition: Chapter3 Node Modules

NPM

Search package

npm search [packagename]

Public package repository

https://www.npmjs.org

Page 13: Node Web Development 2nd Edition: Chapter3 Node Modules

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

Page 14: Node Web Development 2nd Edition: Chapter3 Node Modules

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

Page 15: Node Web Development 2nd Edition: Chapter3 Node Modules

NPM

Eliminate the duplicate modules

npm deduce

List your installed packages

npm list

npm list -g

Page 16: Node Web Development 2nd Edition: Chapter3 Node Modules

NPM

Go the folder of the installed package

npm explore express

npm explore express -g

Page 17: Node Web Development 2nd Edition: Chapter3 Node Modules

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"}

Page 18: Node Web Development 2nd Edition: Chapter3 Node Modules

Build your package

Before publishing your package, test it

npm link

Create a symbolic link in your system-wide modules

Publish

npm publish

Page 19: Node Web Development 2nd Edition: Chapter3 Node Modules

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]

Page 20: Node Web Development 2nd Edition: Chapter3 Node Modules

Configuration settings

List your configuration settings

npm config list

Delete the key

npm config delete [key]

Page 21: Node Web Development 2nd Edition: Chapter3 Node Modules

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