Introduction to NPM

NPM is commonly used in the following scenarios:

  • Allows users to download third-party packages written by others from the NPM server for local use.
  • Allows users to download and install command line programs written by others from the NPM server for local use.
  • Allows users to upload their own packages or command-line programs to the NPM server for others to use.

Installation Node. Js

  1. First go to the Node.js download page to get the version you need.
  2. Viewing the Installed Version
    $node - version v6.10.3Copy the code
  3. To verify that we have installed correctly, go into the Node’s REPL and try it
    $ node
    > console.log('Node is running');
    Node is running
    > .help
    .break Sometimes you get stuck, this gets you out
    .clear Alias for .break
    .exit  Exit the repl
    .help  Show repl options
    .load  Load JS from a file into the REPL session
    .save  Save all evaluated commands in this REPL session to a file
    > .exit
    Copy the code
  4. Node.js has been installed, and NPM has been installed along with Node.js.
    $NPM - version 3.10.10Copy the code
  5. NPM packages can be installed locally or globally
    npm install express          # Local install
    npm install express -g   # global install
    Copy the code

    If the following error occurs:

    npm err! Error: connect ECONNREFUSED 127.0.0.1:8087 
    Copy the code

    The solutions are:

    $ npm config set proxy null
    Copy the code
  6. The local installation
    • /node_modules (the directory where the NPM command is run). If there is no node_modules directory, the node_modules directory will be generated in the directory where the NPM command is run.
    • You can import locally installed packages by requiring ().
  7. Global installation
    • Place the installation package in /usr/local or your Node installation directory.
    • It can be used directly from the command line.
  8. Viewing Installation Information
    • Use the following command to view all globally installed modules:
    $NPM list - g ├ ─ ┬ [email protected] │ ├ ─ ─ [email protected] │ ├ ─ ─ [email protected] │ ├ ─ ─ [email protected] │ ├ ─ ┬ [email protected] │ │ └ ─ ─ [email protected] │ ├ ─ ┬ [email protected] │ │ └ ─ ─ [email protected]...Copy the code
    • To view the version number of a module, run the following command:
    $NPM list projectName@projectVersion /path/to/project/ Folder ├ ─ [email protected]Copy the code

Uninstall the module

  • Use the following command to uninstall the Node.js module.
    $ npm uninstall express
    Copy the code
  • After uninstalling, you can go to /node_modules/ to see if the package still exists, or use the following command:
    $ npm ls
    Copy the code

The update module

    $ npm update express
Copy the code

Search module

   $ npm search express 
Copy the code

Create a module

$ npm init
Copy the code

Use taobao NPM image

Domestic direct use of NPM official mirror is very slow, recommended to use Taobao NPM mirror. You can use taobao’s customized CNPM (gzip compression support) command line tool instead of the default NPM:

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

This allows you to install the module using the CNPM command