Reading this npm article will help you take 50 steps less than your peers.

This article mainly talks about npm, focusing on what npm is, when to use npm, some commonly used instructions, and some personal suggestions for newcomers to use npm.

what is npm

NPM is a package management tool installed along with NodeJS, which can solve many problems in NodeJS code deployment.

composition

The official explanation is this:
npm consists of three independent parts:

  • The website is the main way for developers to find packages, set parameters, and manage the npm experience.
  • The registry is a huge database that stores information about each package.
    Command line tools (CLI) are run via the command line or terminal. Developers interact with npm through the CLI.

When to use npm

  • Users download third-party packages written by others from the NPM server and use them locally.
  • Users download and install command line programs written by others from the NPM server for local use.
  • Users upload packages or command line programs they write to the NPM server for others to use.

Use npm essential commands

1. If there is no node_modules folder under the project you are connected to, use npm init -y to initialize webpack and generate the default package.json
2. If there is already a node_modules folder under the project you are connected to, import the dependency package npm i to generate the default node_modules file Folder
3. If you want to start over, we can use the quick delete node_modules folder command

npm install -g rimraf
rimraf node_modules
  1. npm installation, update, and deletion of dependent packages
  • (1)Installation
npm install xxx  // 安装但不写入package.json
npm install xxx –S // 安装并写入package.json的"dependencies"中
npm install xxx –D // 安装并写入package.json的"devDependencies"中
npm install xxx -g // 全局安装
npm install xxx@1.2.0 // 安装指定版本,后面写版本号
yarn global add xxxx //全局安装 
  • (2)Delete
npm uninstall 模块名字 // 删除指定模块 -S -D -g,后面加上这些对应上面的注释,相当于加入对应的文件中
  • (3)Update
    Check for updates first
npm outdated

Execute the above command to see all modules that can be updated.
We need to update the package.json file first:
install the "npm-check-updates" module

npm install -g npm-check-updates

Check for updatable modules

//1.
npm-check-updates
//2.
ncu

Both of the above commands can check for updateable modules. Next, update the dependency packages of package.json to the latest version.

ncu -u

Execute the above command to update all modules. However, in actual development, it is not recommended to update all at once. You can update the specified modules according to actual needs, and you can add -D, -S or -g at the end according to the scope.

npm update xxx

Note: Specifying updates requires modifying the version number in package.json in advance.
Of course, to be on the safe side, after updating package.json, it is recommended to delete the entire node_modules directory and re-initialize project
5. As mentioned above, version number can be viewed with npm -v.
Taobao mirror

npm install -g cnpm --registry=https://registry.npm.taobao.org
npm config set registry https://registry.npm.taobao.org

personal suggestion

  1. Before installation, be sure to remember npm init -y to initialize during installation.
  2. When using other people's code, make two copies. When you can understand it, make one copy with npm-i recovery and one copy with npm install -g rimraf. Delete the node_modules folder and use the command to reinitialize and reinstall. Beginners are advised to do this. You can learn in the process. to a lot
  3. Don't panic when you report an error. Most people panic when they report an error. They think English is difficult to understand. In fact, most of the places where errors are reported are similar. If it doesn't work, there is a translator. Take a look at it yourself first. Don't rush.
  4. So if you have checked your code and think there is no problem, look at the word writing
  5. If you still can't find it, you can look at the official documents and ask Baidu to see if there are any friends who have fallen into the trap and how they did it. Based on my experience of countless trials and errors, Baidu can solve 85% of the problems. There are still 15% problems, 14% are writing problems, and 1% are logic problems
    *I wish everyone can use it smoothly~~~~ *
    You also have the remaining 49 steps

Guess you like

Origin blog.csdn.net/Yannnnnm/article/details/111483493