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 must be installed in every project that uses the software package. Local packages, 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 a 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 to npm install.
You can add the -g option to most npm commands by working with 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've seen come with an optional -g flag indicating that you are using global modules.
Examples are as follows
The following code installs the browserify package globally.
npm install -g browserifyThis puts browserify on the command line, which we used in the previous chapter.
Updated global package
npm update -g package-nameList global packages
npm ls -gUninstall package
npm rm -g package-nameFor example, to uninstall Browserify, run
npm rm -g browserifyNPM does not modify the system configuration when installing modules globally.
Global modules are placed on the command line where they are available.
Use global module require
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 project (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) and create an app.js as shown.
Recommended learning: "nodejs video tutorial"
The above is the detailed content of what is the global package in node. For more information, please pay attention to other related articles on this site!