Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand the NPM knowledge points of NodeJS

2025-01-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article will explain in detail how to understand the NPM knowledge points about NodeJS. The content of the article is of high quality, so the editor will share it with you for reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.

What is NPM?

Before we understand NPM, we need to understand what are the packages and modules in NodeJS?

To put it simply, a module is a separate file, and there can be one or more modules in a package.

In NodeJS, in order to facilitate developers to publish, install and manage packages, NodeJS has launched a package management tool NPM (Node Package Manager).

NPM does not need to be installed separately, as long as the NodeJS environment is set up, it will be installed automatically.

NPM is like a software butler on a computer. Through NPM, we can quickly find the packages we need, quickly install the packages we need, quickly delete the packages we don't want, and so on.

Installation of NPM using the NPM package

1. Global installation

Global installation is generally used to install tools used globally and is stored in the global node_modules.

# installation package

Npm install-g package name # install the latest version by default

# Uninstall the package

Npm uninstall-g package name

# Update package

Npm update-g package name # you can use install directly if you failed to update

2. Local installation

Local installation is generally used to install the packages used by the current project, which are stored in the current project node_modules

# installation package

Npm install package name

# Uninstall the package

Npm uninstall package name

# Update package

Npm update package name

About package.json in NPM

When we create a project, there is no package.json file in our project directory, so we can generate it automatically after initializing the local package.

Npm init

We can open this file to see the contents of the file.

The various modules required by the current project are defined in the package description file package.json, as well as the configuration information of the project (such as name, version, license, etc.). According to this configuration file, the npm install command automatically downloads the required modules, that is, the running and development environment required to configure the project.

Note: no comments can be added to the package.json file

There are two configuration items in package.json that we should pay attention to:

Dependencies: production environment package dependencies, an associative array consisting of package name and version number devDependencies: development environment package dependencies, an associative array consisting of package name and version number

When we install the package, we use the npm install package name-the package name of the save installation is reflected in the dependencies configuration item.

When we install the package, we use the npm install package name-the package name of the save-dev installation is reflected in the devDependencies configuration item.

When we copy the project to others, or release it, we will not give the node_modules in the project to others, because it is too large, and some packages may only be needed in the development phase, but not in the launch phase, so we need to specify them separately.

So when you release the project, you can use the following command to configure the corresponding environment

Npm I all packages will be installed

Npm I-- production only installs packages in dependencies

Npm I-- development only installs packages in devDependencies

This is where NPM is convenient.

What if it is too slow to download the Node package?

Many readers are faced with a problem when using NPM, that is, it is too slow to install packages using NPM. You can try using nrm.

Method one-nrm

Because npm goes back to download resources abroad by default, it will be slow for domestic developers to download resources, so someone has written a nrm tool that allows you to change the download address of resources from abroad to China.

You can try the following command for specific use

Npm install-g nrm # install NRM

Nrm-- version # to see if the installation is successful

Npm ls # View the addresses of resources allowed to switch

Npm use taobao # changes the download address to Taobao

Use the npm install package name directly.

Method 2-cnpm

Cnpm is to switch the download source from abroad to domestic download, but to change all instructions from npm to cnpm.

Npm install cnpm-g-registry= https://registry.npm.taobao.org # install CNPM

Cnpm-v # to see if the installation is successful

Use it in the same way as npm. For example, you can change npm install jquery to cnpm install jquery.

In fact, in addition to npm, Facebook, Google, Exponent and Tilde jointly launched a new JS package management tool, Yarn.

Yarn was created to make up for some of the flaws before npm5.0, but npm is enough for crawler engineers.

How to understand the NPM knowledge of NodeJS is shared here. I hope the above content can be of some help to you and can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report