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
:
|
|
package.json includes four main parts: basic module info, dependencies, devDependencies, scripts:
1234567891011121314151617181920212223242526// 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 commands12345678$ 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
123"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
123456$ npm set init-author-name 'Tony'$ npm get init-author-nameTony$ 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.jsonInstall single module, for shorhands: https://docs.npmjs.com/misc/config
123456789$ 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-devRemove
123$ 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
|
|
Package official github repo
|
|