npm usage tutorial

Posted by SBro on Sun, 05 May 2019 23:40:02 +0200


What's this

npm is to Node.js, just as pip is to Python, gem is to Ruby, and pear is to PHP.
npm is the official package management tool provided by Node.js. It has become the standard platform for Node.js package publishing, dissemination and dependency control. npm provides command-line tools to make it easy to download, install, upgrade, delete packages, and to publish and maintain packages as developers.


npm is a package management tool installed with Node.js, which can solve many problems in Node.js code deployment. There are several common scenarios:

Allow users to download third-party packages written by others from npm servers for local use.
Allow users to download and install command-line programs written by others from npm servers for local use.
Allow users to upload packages or command-line programs written by themselves to npm servers for others to use.

Behind npm is a database based on couchdb, which records the information of each package in detail, including author, version, dependency, authorization information, etc. One of its important functions is to free developers from the tedious package management (version, dependency, etc.) and focus more on the development of functions.

How to use it?

npm does not need to be installed separately. When Node is installed, npm is installed together. However, the npm attached to Node may not be the latest version. Finally, update to the latest version with the following command.

// Window
npm install npm -g
// Linux
$ sudo npm install npm@latest -g

npm init

npm init is used to initialize and generate a new package.json file. It asks users a series of configuration-related questions. If you don't think you need to change the default configuration, you can go all the way back.


  "name": "smartadmin-angularjs-app",
  "version": "1.8.1",
  "description": "Demo Application for AngularJS version of SmartAdmin application template",
  "main": "index.html",
  "private": true,
  "scripts": {
    "dev": "gulp dev",
    "prod": "gulp prod"
  "author": "Sunny",
  "license": "MyOrange Inc.",
  "devDependencies": {
    "gulp": "^3.9.0",
    "gulp-concat": "^2.6.0",
    "gulp-angular-templatecache": "^1.7.0",
    "gulp-connect": "^2.2.0",
    "gulp-ng-annotate": "^1.0.0",
    "gulp-uglify": "^1.2.0",
    "event-stream": "~3.3.1",
    "lodash": "^4.0.0"


Through the strategy of code over configuration, Gulp makes simple tasks simple and complex tasks manageable.
With the power of Node.js streams, you can quickly build projects and reduce frequent IO operations.
Gulp's strict plug-in guidelines ensure that plug-ins work as concisely and qualitatively as you expect.

var gulp = require('gulp');
gulp.task('default', function() {
  // Put your default task code here

code snippet

gulp.task('dev', ['vendor', 'js', 'watch', 'connect']);
gulp.task('default', ['dev']);

gulp.task('js', function () {
    return es.merge(gulp.src(source.js.src), getTemplateStream())

gulp.task('watch', function () {, ['js']);, ['js']);

gulp.task('connect', function () {
        port: 16701

gulp.task('vendor', function () {
    _.forIn(scripts.chunks, function (chunkScripts, chunkName) {
        var paths = [];
        chunkScripts.forEach(function (script) {
            var scriptFileName = scripts.paths[script];

            if (!fs.existsSync(__dirname + '/' + scriptFileName)) {

                throw console.error('Required path doesn\'t exist: ' + __dirname + '/' + scriptFileName, script)
            .pipe(concat(chunkName + '.js'))
            //.on('error', swallowError)


Topics: node.js npm AngularJS pip Python