Global installation
Installation position
Global installation is to install the NPM package in the node_modules folder of your Node installation directory. The default path for global installation is different on Windows and MAC. The default installation on MAC is /usr/locla/lib. The default installation directory on Windows is C:\Program Files\nodejs, of course you can also check the global installation directory by using the following command.
// Check the global installation path NPM root -g // Check the basic NPM Settings NPM config ls // Check the installation directory path NPM config get prefixCopy the code
Global command
After the NPM package is installed globally, the package commands are registered globally and you can execute the commands directly from the command line. When you install an NPM package globally, the package is stored in /usr/locla/lib/node_modules or C: Program Files\nodejs\node_modules. In the package.json file, the execution commands configured under the bin property are placed in /usr/locla/bin or C: Program Files\nodejs. When you execute this command from the command line, the system will execute the corresponding file in the /usr/locla/bin directory.
The installation process
Take the global installation VUE-CLI as an example to describe the installation process.
npm install -g @vue/cli
Install vue package to/usr/locla/lib/node_modules
.
- Look for the bin property in package.json of the vue package in node_modules.
- In the directory
/usr/locla/bin
orC:\Program Files\nodejs
View the vue command execution file
- Execute on the command line
vue create vue-test
The local installation
Installation position
If you execute NPM install XXX in a particular project, the package will be installed in the node_moduels directory for that project. However, if you execute the command directly from the package in this project, you will find an error from the console telling you that the command could not be found. There are two solutions:
- Use NPX execution: The main problem NPX solves is to call modules installed inside the project, so you can execute in the project
NPX package command
. - Configure in package.json file:
"scripts": {
"Package command": "Package command",}Copy the code
How it works: After a package is installed locally, the commands for that package are added to the project’s node_modules/.bin file. When you run the NPM run command, scripts in package.json look for commands in a certain order, and local node_modules/.bin is also in the search list. So locally installed package commands can be executed.