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.
- A command line tool: npm
- a registered repository: npm registry (can have a private library, for example, the domestic ali built a, convenient for domestic use)
- npm company, yes, it is a company
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 is actually a matter of installing nodejs. When you install nodejs, you install npm along with it.
[[email protected].io]# curl -fsSL https://deb.nodesource.com/setup_lts.x | sudo -E bash -
[[email protected].io]# sudo apt update
[[email protected].io]# apt intall nodejs
Wait for the installation to complete and you can use.
[[email protected].io]# npm --version
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.
[[email protected].io]# npm init
[[email protected].io]# npm install normalize.css
Here I added a dependency via npm:
normalize.css, and then look at
[[email protected].io]# cat package.json
"test": "echo \"Error: no test specified\" && exit 1"
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
[[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: ``
[[email protected].io]# tree
│ └── normalize.css
│ ├── CHANGELOG.md
│ ├── LICENSE.md
│ ├── normalize.css
│ ├── package.json
│ └── README.md
ok, it’s that simple.
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.