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 use Node.js package Manager npm

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces how to use Node.js package manager npm, which is very detailed and has certain reference value. Friends who are interested must finish reading it!

Purpose

The current Node.js installation package comes with an important toolkit manager, npm. Npm has two main functions: downloading and managing third-party modules and building and running projects. The use of npm itself is not complicated, but it is not easy to use it in the network environment of the mainland, which only adds a lot of work. This article will give an explanation of the relevant content.

Npm init and package.json files

There is usually a package.json file under the project root of Node.js, which mainly configures project-related information, including project name, version number, entry file, required modules, and so on. This file can be created manually, but it is usually generated with the npm init command (you can also use npm init-y to skip the query phase to generate a package.json file with default parameters):

Usually a package.json file contains many fields, such as the following:

{"name": "naisu", "version": "1.0.0", "description": "lalala", "main": "index.js", "scripts": {"start": "node index.js"}, "author": "nx", "license": "ISC", "dependencies": {"electron-squirrel-startup": "^ 1.0.0" "serialport": "^ 9.2.4"}, "devDependencies": {"electron": "15.1.1", "electron-rebuild": "^ 3.2.3"}}

Some of these fields are relatively important, as described below:

Name Project name

Version project version number

Main project main entry file

Scripts npm run command script

For example, if "start": "node index.js" is configured above, we can use npm run start to execute node index.js commands in the terminal (start/stop/test/restart these four commands can omit run), which is very useful when complex commands need to be executed.

Modules that the dependencies project depends on to run

Modules required for devDependencies project development

These two fields hold the name and version of the module that the project depends on. With this information, you can use npm install to install these modules into the project.

Module installation and management installation module

The module is installed using npm install, for example, the cowsay module is installed in the following demonstration:

When npm installs the module, it will install the dependent module at the same time. By default, it will be installed in the node_modules folder in the directory, and the installed module will be recorded in the dependencies field of the package.json file. At the same time, the version information of the module and the dependent module will be recorded in the package-lock.json file.

You can install a specified version, such as npm install cowsay@1.5.0, by adding the @ version number to the module name during installation.

If a module is already installed, it will not be reinstalled when you execute the installation command again, and you can use the-f or-- force option to force the installation to handle: npm install-- force.

At installation time, you can use the-S or-- save option to mark the module as dependent on which the dependencies project runs (the default), or you can use the-D or-- save-dev option to mark the module as required for devDependencies project development, such as npm install-- save-dev.

Modules can also be installed to the global catalog, such as npm install-- global, using the-g or-- global options.

The install in the installation module command can also be abbreviated to I.

View installed Modul

Use npm list to view installed modules and their dependencies in the current directory.

Use the-g or-- global options to view module information for the global installation, and-- depth=x to specify the viewing depth.

Update module

Npm update can be used to update modules, and options such as-- save--save-dev-- global mentioned above can also be used here.

Delete module

Use npm uninstall to uninstall the module.

Use the-g or-- global options to uninstall the global module.

Npx

Npx is a tool in the new version of npm (since 5.2), which is mainly used to run the module: if there is the module in the project directory or system environment, then run the module directly, if not, it will not be installed, npx will download it to a temporary directory and then run it.

Module compilation

Some third-party modules are developed in other languages, which cannot be run directly and need to be compiled for the runtime environment before they can be used, which requires compilation tools, such as node-gyp and node-pre-gyp tools. For some modules, other tools may be required on some platforms, such as windows-build-tools, which is often needed on the windows platform.

These tools can be installed and compiled when needed, and most of the time npm handles these things automatically. Some platforms can also install these tools when installing Node.js. For example, on windows platforms, you can check to install these tools when installing Node.js:

It installs these tools after installing Node.js:

It's okay if you don't check this when you install Node.js at the beginning, or you can reinstall it.

Version control

The module-related information is recorded in both the package.json and package-lock.json files that appear earlier, and one of the important information is the version number of the module.

The version number of a module or project in Node.js consists of three numbers separated by dots, followed by major, minor, and patch versions from left to right.

The version number rule itself is nothing special, but there are many modifiers before and after the version number in npm management and package.json and package-lock.json files. These symbols specify the rules for module installation and update. Common rules are as follows:

No retouching: specify a specific version, such as 2.2.3

Latest: use the latest version available

^: only updates that do not change the leftmost non-zero number will be performed

~: only the patch version will be updated

>: accept any version higher than the specified version

2.6

Change the source

In the mainland network environment, there is often an error or failure in the download and installation of the module, resulting in the program not running correctly. You can try the following steps to reinstall:

Clear cache npm cache clean-- force

Delete the node_modules folder and its contents

You can delete a package-lock.json file if you have it (remember to back it up)

Reinstall the module (if you have a package.json file, you can install all modules in one step as long as npm install is available)

If the above method doesn't work, and it doesn't work without a ladder or a ladder, you have to try to change the source.

Use the nrm tool to switch sources

The most convenient way to change the source of npm is to use the nrm tool, which can be installed globally using npm install-g nrm, or directly using npx nrm. The main operations commonly used in nrm are as follows:

Nrm ls lists available sources and addresses

Nrm test test available source speed

Nrm use switching source

Add add Source

Del delete source

Use cnpm instead of npm

Cnpm can be used to replace most of the work of npm, but it uses Taobao's mirror source. For more information, see: https://npmmirror.com/

You can use npm install-g cnpm-- registry= https://registry.npmmirror.com to install cnpm. When you use it later, you just need to change the place where you need npm to cnpm.

The above is all the contents of the article "how to use Node.js package Manager npm". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

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: 221

*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

Development

Wechat

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

12
Report