This is the 16th day of my participation in the August More Text Challenge. For details, see: August More Text Challenge

NPM is called Node Package Manager.

There are two benefits to using NPM

  1. Unified download entry
  2. Automatic download dependencies
I. Installation of NPM

Just install nodeJS and the NPM tool comes with it

Nodejs can be downloaded from nodejs.org/en/download…

After the installation is complete, enter NPM -v on the CLI. If the installation is successful, the version number of NPM is displayed

/ / NPM version
npm -v
6.9. 0
// You can also look at the node version
node -v
v1016.. 0
Copy the code

After NPM is installed successfully, run the NPM install npm@version command to upgrade NPM to a specified version. You can also run the @latest command to indicate that the installed version is the latest stable version. Sometimes, we want to install software globally, in the global environment, enter this command in any directory can be recognized, this is by adding the -g flag, indicating the global installation

npm install npm@latest -g
Copy the code

In a project, NPM is initialized with NPM init -y; -y means to initialize NPM with the default configuration, so you don’t have to ask me any more questions

After initialization, a package.json file is generated in the current directory, which is essentially a standard JSON object and can be modified

{
  "name": "demo"."version": "1.0.0"."description": ""."main": "index.js"."scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": []."author": ""."license": "ISC"
}
Copy the code

When you enter the installation command, install can be abbreviated to I

// Install jQuery
npm i jquery
// For example, install vue
npm i vue
Copy the code

When NPM receives the installation command, it first checks to see if there is a plugin of that name. If there is no plugin of that name, it will issue a warning. If there is a plugin, it will download it and install it to the specific directory node_modules

In addition, after the plug-in is installed, the information corresponding to the installed plug-in is also written to package.json

{
  "name": "demo"."version": "1.0.0"."description": ""."main": "index.js"."scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": []."author": ""."license": "ISC"."dependencies": {
    "bootstrap": "^ 4.3.1." "."jquery": "^ 3.4.1 track"."vue": "^ 2.6.10"}}Copy the code

If node_modeles is deleted and you need to retrieve all dependencies, you can use the NPM I command to reinstall the plugins using the information in package.json

npm i
Copy the code

Notice you don’t have to add anything after that, which means you’re reinstalling all the libraries, okay

In other cases, you may need to delete a plug-in with a specific name. You can use the NPM uninstall plug-in name command to do so

/ / remove the jQuery
npm uninstall jquery
Copy the code

Note that when you uninstall a plug-in using the NPM uninstall plug-in name command, the plug-in is removed in node_modules, and the dependencies in package.json are removed in dependencies

In addition, the plug-in version can be updated with the NPM Update plug-in name

npm update jquery
Copy the code

If you want to update to an older version, you can use @ to specify the version number

NPM update [email protected]Copy the code

Finally, take a closer look at the contents of package.json, the common configuration items for NPM

  1. package name: Package name. Note: The package name must not contain Spaces and must be lowercase.
  2. version: Package version number, default is 1.0.0
  3. description: Description of the package, either written or unwritten
  4. entry point: The package entry file
  5. test commandCommand:
  6. git repository: git repository
  7. keywords: the key word
  8. authorAuthor:

When you initialize NPM with NPM init, you will generate a JSON file whose contents can be configured during initialization or modified directly later

Again, there is a script in JSON that specifies shortcuts to some commands

{
  "name": "demo"."version": "1.0.0"."description": ""."main": "index.js"."scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": []."author": ""."license": "ISC"
}
Copy the code

As above, then we can run the command defined above; We can also customize some commands in this script. If you specify a command, you can run any command

 npm run test
Copy the code

These are some basic uses of NPM!