NPM Playbook

NPM (node package manager) is a package management tool for Node.js.
Node.js is an open source JavaScript runtime built on Chrome’s V8 JavaScript engine. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient. Note that Node.js is a server side runtime environment rather than a language.

Initial project

package.json will be firstly created by npm init:

1
$ npm init # create package.json
  • package.json includes four main parts: basic module info, dependencies, devDependencies, scripts:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    // package.json
    {
    "name": "nodexpress",
    "version": "1.0.0",
    "description": "",
    "main": "app.js",
    "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "node app.js"
    },
    "author": "tony",
    "license": "ISC",
    "dependencies": {
    "express": "^4.13.3"
    },
    "devDependencies": {
    "gulp": "^3.9.0",
    "gulp-inject": "^1.5.0",
    "gulp-jscs": "^2.0.0",
    "gulp-jshint": "^1.11.2",
    "gulp-nodemon": "^2.0.6",
    "jshint-stylish": "^2.1.0",
    "wiredep": "^3.0.0"
    }
    }
  • scripts inside packages.json to run shell commands

    1
    2
    3
    4
    5
    6
    7
    8
    $ npm start will run $ node xx.js
    $ npm run xxx for custom commands
    // packages.json
    "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "node app.js"
    },
  • versioning packages

    1
    2
    3
    "express": "^4.13.3" => will install the latest "4.XX.X" verison
    "express": "~4.13.3" => will install the latest "4.13.X" verison
    "express": "4.13.3" => will keep this version
  • default settings for package.json

    1
    2
    3
    4
    5
    6
    $ npm set init-author-name 'Tony'
    $ npm get init-author-name
    Tony
    $ npm config delete init-author-name
    $ cat ~/.npmrc # all the default settings will be saved inside this file.
    init-author-name=Tony

Install and uninstall packages

Search nodejs packages: https://www.npmjs.com/

  • Project with packages.json can install(update) modules easily:

    1
    $ npm install # automatically install dependencies inside packages.json
  • Install single module, for shorhands: https://docs.npmjs.com/misc/config

    1
    2
    3
    4
    5
    6
    7
    8
    9
    $ npm install express --save # save dependency into package.json
    $ npm install gulp --save-dev # save devDependency into package.json
    $ npm install bower -g # install global, so can call bower inside terminal
    $ npm install express@"4.2.x" --save # install specific version
    $ npm install underscore@">=1.1.0 <1.4.0" --save
    $ npm i express -S # Shorthands for --save
    $ npm i gulp -D # for --save-dev
  • Remove

    1
    2
    3
    $ npm uninstall [-g] underscore --save # remove dependency from pkg.json
    $ npm prune # add extraneous pkg into pkg.json
    $ npm prune --production # rm dev dependencies from pkg.json
  • Upgrade npm

    1
    $ npm i npm@latest -g

List installed packages

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
$ npm list --depth 0
nodexpress@1.0.0 /Users/tony/Hacker/nodexpress
├── express@4.13.3
├── gulp@3.9.0
├── gulp-inject@1.5.0
├── gulp-jscs@2.0.0
├── gulp-jshint@1.12.0
├── gulp-nodemon@2.0.6
├── jshint-stylish@2.1.0
└── wiredep@3.0.0
$ npm list --depth 0 --prod true # production dependencies packages only
nodexpress@1.0.0 /Users/tony/Hacker/nodexpress
└── express@4.13.3
$ npm list --depth 0 --dev true # devDependencies packages only
nodexpress@1.0.0 /Users/tony/Hacker/nodexpress
├── gulp@3.9.0
├── gulp-inject@1.5.0
├── gulp-jscs@2.0.0
├── gulp-jshint@1.12.0
├── gulp-nodemon@2.0.6
├── jshint-stylish@2.1.0
└── wiredep@3.0.0
$ npm list --global true --depth 0 # global packages
/usr/local/lib
├── bower@1.7.2
├── gulp@3.9.0
├── hexo-cli@0.2.0
└── npm@3.3.12
$ npm list --depth 0 --long true # detail info
$ npm list --depth 0 --json true # detail info with json format

Package official github repo

1
$ npm repo underscore # Jump to browser and open github repo
Contents
  1. 1. Initial project
  2. 2. Install and uninstall packages
  3. 3. List installed packages
  4. 4. Package official github repo
|