Latest web development tutorials

NPM use Introduction

NPM is accompanied NodeJS package management tools installed together, can solve many problems NodeJS code for deployment on common usage scenarios are the following:

  • NPM allows users to download from the server to the third-party packages written by others for local use.
  • It allows users to download and install the command-line program written by someone else to use the local server from the NPM.
  • It allows users to write their own package or command line program uploaded to the server for others to use NPM.

Since the new version of nodejs have integrated npm, so before npm also be installed. You can also enter the"npm -v" to test whether a successful installation.The following command, version appears prompt installation was successful:

$ npm -v
2.3.0

If you install an older version of npm, it can easily be upgraded via npm to command, the command is as follows:

$ sudo npm install npm -g
/usr/local/bin/npm -> /usr/local/lib/node_modules/npm/bin/npm-cli.js
[email protected] /usr/local/lib/node_modules/npm

If Window system, use the following command:

npm install npm -g

Npm install using the command module

npm install Node.js module syntax is as follows:

$ npm install <Module Name>

The following examples, we use the command to install npm common framework Node.js web moduleexpress:

$ npm install express

Once installed, express the packet is placed in the node_modules directory in the project directory, so the code only throughrequire ( 'express') like manner, without specifying the path to third-party packages.

var express = require('express');

Global installation and local installation

npm install package into a local installation (local), global installation (global) are two, knock from a command line, the only difference is there is no -g only, such as

npm install express          # 本地安装
npm install express -g   # 全局安装

If the following error:

npm err! Error: connect ECONNREFUSED 127.0.0.1:8087 

Solution:

$ npm config set proxy null

Local Installation

  • 1. Place the installation package placed in the ./node_modules (directory where the command is running npm), if not node_modules directory will generate in the current directory node_modules npm command execution directory.
  • 2. can be introduced locally installed package through require ().

Global Installation

  • 1. Place the installation package placed in the / usr / local or your installation directory node.
  • 2. You can use directly on the command line.

If you want to have both features, you need to install it in two places or use npm link.

Next, we use the global install express

$ npm install express -g

The installation process the following output, the first line of output version number and installation location of the module.

[email protected] node_modules/express
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected] ([email protected])
├── [email protected] ([email protected])
├── [email protected] ([email protected], [email protected])
├── [email protected] ([email protected])
├── [email protected] ([email protected], [email protected])
├── [email protected] ([email protected], [email protected])
└── [email protected] ([email protected], [email protected], [email protected], [email protected], [email protected])

You can use the following command to view all global installed modules:

$ npm ls -g

Use package.json

package.json located in the directory module, defines the properties of the package. Let's look at the file package.json express package located node_modules / express / package.json content:

{
  "name": "express",
  "description": "Fast, unopinionated, minimalist web framework",
  "version": "4.13.3",
  "author": {
    "name": "TJ Holowaychuk",
    "email": "[email protected]"
  },
  "contributors": [
    {
      "name": "Aaron Heckmann",
      "email": "[email protected]"
    },
    {
      "name": "Ciaran Jessup",
      "email": "[email protected]"
    },
    {
      "name": "Douglas Christopher Wilson",
      "email": "[email protected]"
    },
    {
      "name": "Guillermo Rauch",
      "email": "[email protected]"
    },
    {
      "name": "Jonathan Ong",
      "email": "[email protected]"
    },
    {
      "name": "Roman Shtylman",
      "email": "[email protected]"
    },
    {
      "name": "Young Jae Sim",
      "email": "[email protected]"
    }
  ],
  "license": "MIT",
  "repository": {
    "type": "git",
    "url": "git+https://github.com/strongloop/express.git"
  },
  "homepage": "http://expressjs.com/",
  "keywords": [
    "express",
    "framework",
    "sinatra",
    "web",
    "rest",
    "restful",
    "router",
    "app",
    "api"
  ],
  "dependencies": {
    "accepts": "~1.2.12",
    "array-flatten": "1.1.1",
    "content-disposition": "0.5.0",
    "content-type": "~1.0.1",
    "cookie": "0.1.3",
    "cookie-signature": "1.0.6",
    "debug": "~2.2.0",
    "depd": "~1.0.1",
    "escape-html": "1.0.2",
    "etag": "~1.7.0",
    "finalhandler": "0.4.0",
    "fresh": "0.3.0",
    "merge-descriptors": "1.0.0",
    "methods": "~1.1.1",
    "on-finished": "~2.3.0",
    "parseurl": "~1.3.0",
    "path-to-regexp": "0.1.7",
    "proxy-addr": "~1.0.8",
    "qs": "4.0.0",
    "range-parser": "~1.0.2",
    "send": "0.13.0",
    "serve-static": "~1.10.0",
    "type-is": "~1.6.6",
    "utils-merge": "1.0.0",
    "vary": "~1.0.1"
  },
  "devDependencies": {
    "after": "0.8.1",
    "ejs": "2.3.3",
    "istanbul": "0.3.17",
    "marked": "0.3.5",
    "mocha": "2.2.5",
    "should": "7.0.2",
    "supertest": "1.0.1",
    "body-parser": "~1.13.3",
    "connect-redis": "~2.4.1",
    "cookie-parser": "~1.3.5",
    "cookie-session": "~1.2.0",
    "express-session": "~1.11.3",
    "jade": "~1.11.0",
    "method-override": "~2.3.5",
    "morgan": "~1.6.1",
    "multiparty": "~4.1.2",
    "vhost": "~3.0.1"
  },
  "engines": {
    "node": ">= 0.10.0"
  },
  "files": [
    "LICENSE",
    "History.md",
    "Readme.md",
    "index.js",
    "lib/"
  ],
  "scripts": {
    "test": "mocha --require test/support/env --reporter spec --bail --check-leaks test/ test/acceptance/",
    "test-ci": "istanbul cover node_modules/mocha/bin/_mocha --report lcovonly -- --require test/support/env --reporter spec --check-leaks test/ test/acceptance/",
    "test-cov": "istanbul cover node_modules/mocha/bin/_mocha -- --require test/support/env --reporter dot --check-leaks test/ test/acceptance/",
    "test-tap": "mocha --require test/support/env --reporter tap --check-leaks test/ test/acceptance/"
  },
  "gitHead": "ef7ad681b245fba023843ce94f6bcb8e275bbb8e",
  "bugs": {
    "url": "https://github.com/strongloop/express/issues"
  },
  "_id": "[email protected]",
  "_shasum": "ddb2f1fb4502bf33598d2b032b037960ca6c80a3",
  "_from": "express@*",
  "_npmVersion": "1.4.28",
  "_npmUser": {
    "name": "dougwilson",
    "email": "[email protected]"
  },
  "maintainers": [
    {
      "name": "tjholowaychuk",
      "email": "[email protected]"
    },
    {
      "name": "jongleberry",
      "email": "[email protected]"
    },
    {
      "name": "dougwilson",
      "email": "[email protected]"
    },
    {
      "name": "rfeng",
      "email": "[email protected]"
    },
    {
      "name": "aredridel",
      "email": "[email protected]"
    },
    {
      "name": "strongloop",
      "email": "[email protected]"
    },
    {
      "name": "defunctzombie",
      "email": "[email protected]"
    }
  ],
  "dist": {
    "shasum": "ddb2f1fb4502bf33598d2b032b037960ca6c80a3",
    "tarball": "http://registry.npmjs.org/express/-/express-4.13.3.tgz"
  },
  "directories": {},
  "_resolved": "https://registry.npmjs.org/express/-/express-4.13.3.tgz",
  "readme": "ERROR: No README data found!"
}

Package.json Property Description

  • name - the package name.

  • version - the version number of the package.

  • description - description of the package.

  • official website url package- homepage.

  • author - author of the package name.

  • Other contributors names package- contributors.

  • dependencies - dependencies list.If the dependencies are not installed, npm will automatically install dependent packages in node_module directory.

  • repository - type package code storage areas, either git or svn, git available on Github.

  • main - main field is a module ID, which is a pointer to a major project of your program.That is, if you pack name express, then the user to install it, and then require ( "express").

  • keywords - Keyword


Unloading module

We can use the following command to uninstall Node.js modules.

$ npm uninstall express

After uninstalling, you can go to the next / node_modules / catalog package is there, or use the following command:

$ npm ls

Update Module

We can use the following command to update the module:

$ npm update express

Search Module

Use the following search modules:

$ npm search express

Create a module

Create a module, package.json file is essential. We can use NPM generate package.json file, the resulting file contains the basic results.

$ npm init
This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sensible defaults.

See `npm help json` for definitive documentation on these fields
and exactly what they do.

Use `npm install <pkg> --save` afterwards to install a package and
save it as a dependency in the package.json file.

Press ^C at any time to quit.
name: (node_modules) w3big                   # 模块名
version: (1.0.0) 
description: Node.js 测试模块(www.w3big.com)  # 描述
entry point: (index.js) 
test command: make test
git repository: https://github.com/w3big/w3big.git  # Github 地址
keywords: 
author: 
license: (ISC) 
About to write to ……/node_modules/package.json:      # 生成地址

{
  "name": "w3big",
  "version": "1.0.0",
  "description": "Node.js 测试模块(www.w3big.com)",
  ……
}


Is this ok? (yes) yes

The above information, you need to input your own situation. After the final input "yes" will generate package.json file.

Then we can use the following command to registered users (use registered mail) in npm repository:

$ npm adduser
Username: mcmohd
Password:
Email: (this IS public) [email protected]

Next, we use the following command to release the module:

$ npm publish

If you do the above steps are done correctly, you can use the same with other modules to install npm.


version number

NPM download and use the code will be released when exposed to the version number. NPM using semantic version number to manage code, here briefly.

Semantics version is divided into three XYZ, representing the major version number, minor version number and patch version number. When the code change, the version number of the update according to the following principles.

  • If only fixes bug, you need to update the Z bit.
  • If you are a new feature, but backward compatible, it needs to be updated Y bits.
  • If there are big changes, not downward compatible, it needs to be updated X position.

With this assurance after the version number, in a statement depend on third-party packages, in addition to rely on a fixed version number, but also depends on a range of version numbers. For example "argv": "0.0.x" depends on the 0.0.x series represents the latest version of argv.

All versions support number range targeting NPM can view the official documentation .


NPM common commands

In addition to the section in this chapter, NPM also provides a lot of features, package.json also has many other useful fields.

In addition to the npmjs.org/doc/ check the official documentation, but then here are some commonly used commands NPM.

NPM provides a lot of commands, such as install and publish, using npm help to see all the commands.

  • NPM provides a lot of commands, such as install and publish , using npm help to see all the commands.

  • Use npm help <command> to view detailed help for a command, such as npm help install .

  • In package.json use the following directory npm install . -g Can be installed locally on the current command line program that can be used for local testing before release.

  • Use npm update <package> can put the current directory node_modules subdirectory inside the module corresponds to the latest version.

  • Use npm update <package> -g can correspond to a command line program globally installed update to the latest version.

  • Use npm cache clear can clear NPM local cache, to deal with the same version number for a new release tags.

  • Use npm unpublish <package>@<version> > can be revoked Post released a version of their own code.


Use Taobao NPM mirror

We all know that domestic direct use of official mirror npm is very slow, there is recommended to use Taobao NPM mirror.

Taobao NPM is a complete npmjs.org mirror image, you can use this instead of the official version (read-only), currently synchronizing frequency of 10 minutes in order to ensure as far as possible to synchronize with the official service.

You can use Taobao custom cnpm (gzip compression support) command-line tool instead of the default npm:

$ npm install -g cnpm --registry=https://registry.npm.taobao.org

This module can be installed using cnpm commands:

$ cnpm install [name]

More information can be found: http://npm.taobao.org/ .