Installation and use of npm

NPM-Node Package Manager-Node Package Manager


1. What is NPM?

Generally, when we say npm, we may refer to 3 things

  1. NPM website: https://www.npmjs.com/
  2. NPM package management library, which stores a large number of JavaScript code libraries
  3. NPM client, the npm command line tool we use. The node.js-based command line tool developed using JavaScript is also a package of Node itself.

NPM official explanation

  • npm is a JavaScript package manager and the largest software registry in the world.
  • A new and powerful way to discover reusable code and integrate code packages into projects.
  • npm makes it easier for JavaScript developers to share and reuse code, and it also makes it easier for us to update the code that is being shared.

Two, npm and nonde.js

  • npm is the default package management system for node.js. After node is installed, npm will be installed by default
  • npm itself is also a package (software) developed based on node.js

3. How to install npm?

  • npm will be automatically installed with nonde.js, and npm will be automatically installed after node.js is installed
  • View the current npm version:npm -v
  • Update npm:npm install npm@latest -g

Fourth, the use of NPM

  1. Find the required package at https://www.npmjs.com/ l website
  2. In the root directory of the project, perform the npm installpackage name installation
  3. require('包名');Load the module in the node.js code
  4. Note: The npm install 包名installation package will automatically download to the directory of the current node_modulesdirectory, if the directory does not exist, it is created, if it exists downloaded directly into it.
  5. By require('包名');loading the module in the code-
    this method mentioned above is called local installation-

Introduction to NPM global installation

  1. What is npm global installation?
    npm install包名 -gNpm global installation refers to installing the package as a command line tool.
//通过npm全局安装mime
npm install mime -g
//安装完毕后可以在命令行中直接使用
mime a.txt命令来查看对应的结果
  1. The npm global installation actually does two things:
    1. Download the package to a specified directory C:\Users\username\AppData\lRoaming \npm\node_modules
    2. Create a section of code to be executed by the command line.c: \Userslusername\AppData\Roaming \npm\mime -> C:\Users\stevexiaohu zhao\AppData\Roaming \npminode_modules imime\cli.js

NPM installation recommendations

The global installation is just to be used as a command line

Five, npm common command introduction

  1. install, install the package.npm install 包名
  2. uninstall, uninstall the package.npm uninstall 包名
  3. version, view the current npm version.npm version或npm -v
  4. init, create a package.json file.npm init
  5. Note: When using npm init -y, if the name of the current folder (directory) is strange (with uppercase, with Chinese, etc.), it will affect npm init -ythe one-step generation operation. At this time, it needs npm initto be generated according to the wizard.

The difference between "Modules" and "Packages"

  1. A module is any file or directory that can be loaded by Node.js’ require()
  • The module can be any file or directory (there can be many files in the directory), as long as it can be passed require() by node.js.
  1. A package is a file or directory that is described by a package. json. This can happen in a bunch ofdifferent ways!
  • A package is a file or directory (there can be multiple files in the directory), and it must be described by a package.json file, and it can be a package.

Guess you like

Origin blog.csdn.net/bealei/article/details/115280564