Overview

Although I use npm from time to time, I’m really not familiar with it, but recently I suddenly wanted to understand what it is and what it does, so I took a little more serious understanding, and this article will record what I understand about npm.

What is npm

First of all, I realized my mistake that npm is all lowercase and all caps are wrong (it’s habitually all caps), although it stands for Node Package Manager, it actually means that it contains three parts.

These are all said on the official website home page. However, generally speaking, when we say npm we are talking about the command line tool for npm, so if it is not specifically stated below, then it means I am talking about the command line tool for npm.

Installing npm

Installing npm is actually a matter of installing nodejs. When you install nodejs, you install npm along with it.

  1. [[email protected].io]# curl -fsSL https://deb.nodesource.com/setup_lts.x | sudo -E bash -
  2. [[email protected].io]# sudo apt update
  3. [[email protected].io]# apt intall nodejs

Wait for the installation to complete and you can use.

  1. [[email protected].io]# npm --version
  2. 7.22.0

What does npm do?

The full name of npm has already been said, it is Node Package Manager, which means package management tool, so what Package does it manage? If you have used Python, then you should know that in Python there is pip, a tool that identifies what other libraries your project depends on based on the requirements.txt file, and what version it has, so that when you get someone else’s repository, you can download the dependencies you need from a copy of the source code and run the project. Similarly, in Go there is go mod, Java has maven, are similar to the existence of.

After understanding this, it is actually to see how npm saves this information, it is very simple, using npm init, npm will create a package.json, in this file, contains information about your current project, and the package your project depends on.

  1. [[email protected].io]# npm init
  2. [[email protected].io]# npm install normalize.css

Here I added a dependency via npm: normalize.css, and then look at package.json: ``

  1. [[email protected].io]# cat package.json
  2. {
  3. "dependencies": {
  4. "normalize.css": "^8.0.1"
  5. },
  6. "name": "npm-learning",
  7. "version": "1.0.0",
  8. "main": "index.js",
  9. "devDependencies": {},
  10. "scripts": {
  11. "test": "echo \"Error: no test specified\" && exit 1"
  12. },
  13. "author": "",
  14. "license": "ISC",
  15. "description": ""
  16. }

Here the dependencies for normalize.css are added and the minimum version is specified, so that when someone else gets my project, they know what dependencies need to be installed and can then run it directly.

The way to install the dependency libraries is

  1. [[email protected].io]# npm install

This will create a node_modules directory in the current directory of the project, which will hold the downloaded dependency packages: ``

  1. [[email protected].io]# tree
  2. .
  3. ├── node_modules
  4. └── normalize.css
  5. ├── CHANGELOG.md
  6. ├── LICENSE.md
  7. ├── normalize.css
  8. ├── package.json
  9. └── README.md
  10. ├── package.json
  11. └── package-lock.json

ok, it’s that simple.

More features

In addition to package management, npm has more features, for example, we are directly in the current project development, writing front-end students know that when the real release, it is necessary to compress the source code, obfuscation and image merging and a series of operations, these can be done through the form of npm + plug-ins, and finally get a dist release file, which can be used as a release This makes the front-end code a little bit safer.

Similarly, back-end node applications can have a similar process, which can be done with npm.

Ref