Introduction to npm package management tool
npm is a package management tool for Node.js, which is used to manage Node.js software packages (including installation, uninstallation, update, etc.). The full name of npm is Node Package Manager.
npm package installation directory under Windows
In the Windows system, the default directory for npm package installation is in the user's AppData directory, such as C:\Users\username\AppData\Roaming\npm.
How to install npm package
npm packages can be installed in the following ways:
1. Local installation
Run the following command in the project root directory:
plaintextCopy code
npm install <package_name>
This will install the specified package in the project's node_modules directory.
2. Global installation
Run the following command in any directory:
plaintextCopy code
npm install -g <package_name>
This will install the specified package in the global npm directory, which can be used anywhere.
3. Install locally and add it to package.json
Run the following command in the project root directory:
plaintextCopy code
npm install <package_name> --save
This will not only install the specified package in the node_modules directory of the project, but also add the information of the package to the dependencies field in the package.json file of the project.
4. Install locally and add it to the devDependencies of package.json
Run the following command in the project root directory:
plaintextCopy code
npm install <package_name> --save-dev
This will not only install the specified package in the node_modules directory of the project, but also add the package information to the devDependencies field in the project's package.json file.
Examples of npm packages
Take the local installation of the express package as an example, you can install it with the following command:
plaintextCopy code
npm install express
You can also add the package to your project's package.json file with the following command:
plaintextCopy code
npm install express --save
After the installation is complete, the express package will appear in the project's node_modules directory. The following code can be used to test whether the installation is successful:
javascriptCopy code
const express = require('express'); const app = express(); app.get('/', (req, res) => { res.send('Hello World!'); }); app.listen(3000, () => { console.log('Example app listening on port 3000!'); });
This code creates an Express application and starts it locally on port 3000. You can check that the application is working by visiting http://localhost:3000.