An NPM package was developed earlier. A problem has been found in practical applications. If you want to execute the package commands directly, then you need to install the package globally. If you install only partially and do not configure it in the package.json file, you will be prompted that the command cannot be found. Why is that? Today I will introduce you to the global installation and local installation exactly what is done,
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. Of course, you can also use the following command to view the global installation path.
// Check the global installation path
npm root -g
// View basic NPM Settings
npm config ls
// Check the installation directory path
npm config get prefix
Copy the code
If you do not want to install the NPM package in the default directory on a public computer, you can change the installation path by using commands.
// Change the installation directory path
npm config set ' '
Copy the code
If you encounter permissions error in global installation, you need to change folder permissions, execute the following command.
sudo chown -R $(whoami) $(npm config get prefix)/{lib/node_modules,bin,share}
Copy 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. In the package.json file, the execution commands configured under the bin property are placed in the /usr/locla/bin file. 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
.
/usr/locla/bin
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.