Getting to know the npm package management tool

npm first time

Recently, I am going to find a modern front-end UI framework. I read it like:

The front-end development I have been in contact with before is html+css+javascript.
If you need to show the tree structure, write your own tree.js or find a js on the Internet for reference.
This method should be regarded as an ancient development method at the moment.

When I first saw npm everywhere, my instinct told me that this is a good thing to learn!
However, I have some deficiencies in my heart. I roughly found some related npm tutorials. I tried to operate it myself, but it was still a little unclear.
Only roughly understand that npm is a package management tool related to the front end.

doubt

What is npm?

npm is a package manager running on node.js developed by Isaaz , the full name is Node Package Manager.
Because in the early stage and nodejs group to warm up, nodejs built npm; later nodejs became popular all over the world, npm also became the standard for front-end sharing code.

Just imagine:
you wrote a tree display plug-in that relies on jQuery 2.0+.
When you share this plug-in with your friends, the jQuery of the friend project is version 1.6.
Then he has to go to the official jQuery website to download JQuery 2.0+ before it can be used normally.
After a while, if you modify the plugin and update the version that jQuery depends on, your friends will beat you (bad-tempered friends).
You don’t have a good way to share when other friends need this plugin.

This is just a problem that may occur with a small plug-in, and then such problems will continue to recur in real projects.

  • Go to jQuery official website to download jQuery
  • Go to BootStrap official website to download BootStrap
  • Other reliance on downloading...
    Development work is these repetitive and meaningless things that squeeze our time to play friends.
    npm must be there to solve some problems.
    It enables you to easily share and borrow packages, and track dependencies and versions.
    And it is so popular now, you can use it all! How can you not use it?

The main components of npm

  • Community website: You don’t need to go to each official website to find the package, URL: npmjs.com.
    Insert picture description here

  • Registry: It is a huge database that saves the basic information of each package.

  • Command line: A tool for developers to deal with npm packages.

How to install npm?

The nodejs software has built-in npm.
To use the npm package management tool, the most common way is to install nodejs on your computer .
There are many complete installation tutorials on the Internet, so I won't go into details here.

How to execute npm commands?

After installing nodejs in the windows system, use the shortcut key win+r to open cmd, enter npm and press enter, there will be a normal prompt, then you can use npm to download resources.
I'll try:

above sea level

You can see that my local npm has been installed
Insert picture description here

above sea level and jquery

Download jquery, without setting the specific version, the default is the latest version.
Insert picture description here
View the download results
Insert picture description here

What is the principle of npm?

The general idea of ​​npm:

  1. Prepare a server to build npm service as a central code repository.
  2. Invite well-known open source authors to upload their works to the central warehouse and perform maintenance updates.
  3. Users search the community for the plug-in they need to use, and record the plug-in name.
  4. The user configures the required plug-in name in package.json.
  5. After executing npm install, npm will download the relevant plug-in package.
  6. Related plugins are stored in the node_modules directory by default and are available for users to use.

Commonly used naming

  • above sea level ls 查看当前目录下安装了哪些node包
    Insert picture description here

  • npm config ls 可查看npm的安装信息以及默认的下载路径
    Insert picture description here

  • npm install 安装nodejs的依赖包

  • npm install -g 将包安装到全局环境中

  • npm install --save 安装的同时,将信息写入package.json中

  • npm init 会引导你创建一个package.json文件,包括名称、版本、作者这些信息等

  • npm remove 移除

  • npm update 更新

  • npm root 查看当前包的安装路径

  • npm root -g 查看全局的包的安装路径

Mirror

Ali has built a mirror server in China: http://npm.taobao.org

Guess you like

Origin blog.csdn.net/u011513460/article/details/105684124