Detailed explanation of the pitfalls of mixing npm and cnpm

Detailed explanation of the pitfalls of mixing npm and cnpm

Have you ever encountered strange problems when using npm and cnpm together? Have you ever encountered the situation where the cnpm installation package on the Alipay mini program is invalid? Are they really just switching a request source?

I believe that many friends use cnpm for a simple purpose, that is, to download things faster, they will change the request source to https://registry.npm.taobao.org
So, we

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

Then use cnpm instead of npm. Once you do this, the problems mentioned above will occur, but you may not be able to discover them for a while.
(In addition, there is also the situation where .npmrc is executed in cnpm. In this case, even if cnpm is used, it will be very slow.)

cause

I installed something using npm, and then found that all the previous ones had problems. I panicked because I was just installing it, so how could it modify the previous ones? So I looked at this problem carefully, but couldn't find the answer, so I asked the big guy.

reason

LinGo's reply

Because cnpm uses soft links by default, it will cause npm to update the previous cnpm package after installation, and then the previous cnpm import will be gg, so a lot of things will be gg.

Then we can find that the reason lies in cnpm itself. If we don't use it, the problem will be solved.

Then the easiest way is to use npm install <some parameters> --registry=https://registry.npm.taobao.org

This is perfect, but it is also very troublesome. You can upgrade it with nrm

Introduction to NPM:

Description: NPM (Node Package Manager) is a package manager for NodeJS, used for node plug-in management (including installation, uninstallation, dependency management, etc.)
Use NPM to install the plugin: Run npm install <name> [-g] [--save-dev] in the command prompt.
<name>: Node plugin name.
Example: npm install gulp-less --save-dev
-g: Install globally. Will be installed in C:\Users\Administrator\AppData\Roaming\npm and written to the system environment variables; Non-global installation: will be installed in the current location directory; Global installation can be called from anywhere on the command line, local installation will be installed in the node_modules folder of the location directory, called by require();
--save: save to package.json (package.json is the NodeJS project configuration file)
-dev;: Save to the devDependencies node of package.json. If -dev is not specified, it will be saved to the dependency node. Why save to package.json? Because the node plug-in package is relatively large, it is not included in the version management. The configuration information is written to the package.json and added to the version management. Other developers can download it accordingly (execute npm install in the command prompt to download all required packages according to package.json).

6. Use npm to uninstall the plugin: npm uninstall <name> [ -g ] [ --save-dev ]

7. Use npm to update the plugin: npm update <name> [ -g ] [ --save-dev ]

8. Update all plugins: npm update [ --save-dev ]

9. View NPM help: NPM help

10. Check the installed plugins in the current directory: npm list

CNPM Introduction:

Note: Because the Google installation plug-in is downloaded from a foreign server, it is greatly affected by the network and may have abnormalities. It would be fine if Google's server was in China, so our Taobao team, who is happy to share, did this. From the official website: "This is a complete npmjs.org mirror. You can use this instead of the official version (read-only). The synchronization frequency is currently once every 10 minutes to ensure synchronization with the official service as much as possible."
Official website: http://npm.taobao.org
Installation: Run npm install cnpm -g --registry=https://registry.npm.taobao.org
in the command prompt. npm install cnpm -g --registry=https://registry.npm.taobao.org
Note: After installation, it is best to check its version cnpm -v or close the command prompt and reopen it. Direct use after installation may cause errors. Note: CNPM is exactly the same as NPM in usage, except that Google is replaced by CNPM when executing the command.

A better way

npm install -g nrm
nrm use cnpm

Then you can happily use npm to quickly download.
nrm

-- The story is not over yet --

At this time, what should I do if cnpm has already been used in the project?

Method improvement

cnpm i --by=npm

That's it, cnpm and npm will not conflict, for example, cnpm i --by=npm react

reason

cnpm using npminstall by default. If you don't like symlink mode for node_modules, you can change the installer to original npm. But you will lose the fastest install speed.

cnpm portal

This is the end of this article about the pitfalls of mixing npm and cnpm. For more information about mixing npm and cnpm, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Windows to implement npm and cnpm installation steps
  • Solution to cnpm not being an internal command: Configure environment variables [recommended]
  • Briefly understand the specific usage of node npm cnpm
  • How to distinguish npm cnpm npx nvm
  • Tutorial on installing nodejs, npm and cnpm on Mac

<<:  Some notes on mysql self-join deduplication

>>:  Complete steps to achieve high availability with nginx combined with keepalived

Recommend

HTML CSS3 does not stretch the image display effect

1. Use the transform attribute to display the ima...

Detailed explanation of how components communicate in React

1. What is We can split the communication between...

js to achieve sliding carousel effect

This article shares the specific code of js to ac...

Do you know the common MySQL design errors?

Thanks to the development of the Internet, we can...

Detailed explanation of MySQL covering index

concept If the index contains all the data that m...

Instructions for recovering data after accidental deletion of MySQL database

In daily operation and maintenance work, backup o...

Vue implements fuzzy query-Mysql database data

Table of contents 1. Demand 2. Implementation 3. ...

What are the advantages of MySQL MGR?

MGR (MySQL Group Replication) is a new feature ad...

js version to realize calculator function

This article example shares the specific code of ...

Difference between varchar and char types in MySQL

Table of contents aforementioned VARCHAR Type VAR...

Three ways to jump to a page by clicking a button tag in HTML

Method 1: Using the onclick event <input type=...

How to install WSL2 Ubuntu20.04 on Windows 10 and set up the docker environment

Enable WSL Make sure the system is Windows 10 200...

Chrome monitors cookie changes and assigns values

The following code introduces Chrome's monito...

vue+element-ui implements the head navigation bar component

This article shares the specific code of vue+elem...