Home > Web Front-end > Front-end Q&A > What is a global package in node

What is a global package in node

WBOY
Release: 2022-07-06 17:28:22
Original
1808 people have browsed it

In node, the global package refers to the tool package used to install local packages in the project, such as nrm, yarn, cnpm, etc.; the software package is a library included in the program, and the software package must be used Install local packages in each project, and global packages only need to be installed in one location. You can use the -g or --global option to command npm to install global packages, or you can use "npm ls -g" to view installed global packages.

What is a global package in node

The operating environment of this article: Windows 10 system, nodejs version 16, Dell G3 computer.

What is a global package in node

Generally, global packages are tool packages, such as: nrm, yarn, cnpm

Software packages are libraries included in the program.

Local packages must be installed in every project that uses them, whereas global packages only need to be installed in one location.

To install a global package, use the -g or --global option command npm install.

You can add the -g option to most npm commands by handling global packages.

To view installed global packages, use the command npm ls -g.

To find the global node_modules folder, use npm ls -g command.

All NPM commands we have seen come with an optional -g flag indicating that you are using global modules.

The example is as follows

The following code installs the browserify package globally.

npm install -g browserify
Copy after login

This kind of placing browserify on the command line, we used it in the previous chapter.

Updated Global Packages

npm update -g package-name
Copy after login

List Global Packages

npm ls -g
Copy after login

Uninstall Packages

npm rm -g package-name
Copy after login

For example, to uninstall Browserify, run

npm rm -g browserify
Copy after login

When installing modules globally, NPM will not modify the system configuration.

Global modules are placed on the command line where they are available.

Using the require of global modules

Globally installed modules should not use the require function in our code, although many packages that support global tags also support local installation in our projects (node_modules folder).

If installed locally, that is without the -g flag, we can use the require function, as we have already seen.

A good simple example is the rimraf module (www.npmjs.org/package/rimraf).

If rimraf is installed globally (npm install -g rimraf), it provides a command line utility that allows you to recursively and forcefully delete directories across platforms.

To delete the directory myData after installing rimraf globally, run

rimraf myData

To do the same thing from Node.js code, install rimraf locally (npm install rimraf), create an app.js as shown in the figure.

What is a global package in node

Recommended study: "nodejs video tutorial"

The above is the detailed content of What is a global package in node. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template