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.
The operating environment of this article: Windows 10 system, nodejs version 16, Dell G3 computer.
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
This kind of placing browserify on the command line, we used it in the previous chapter.
Updated Global Packages
npm update -g package-name
List Global Packages
npm ls -g
Uninstall Packages
npm rm -g package-name
For example, to uninstall Browserify, run
npm rm -g browserify
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.
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!