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

Tomcat maxPostSize setting implementation process analysis

1. Why set maxPostSize? The tomcat container has ...

CSS style reset and clear (to make different browsers display the same effect)

In order to make the page display consistent betwe...

Vue implements drag progress bar

This article example shares the specific code of ...

Implementation steps of vue-element-admin to build a backend management system

Recently, when I was working on a conference heal...

Node.js sends emails based on STMP protocol and EWS protocol

Table of contents 1 Node.js method of sending ema...

Introduction to the use of CSS3 filter attribute

1. Introduction When writing animation effects fo...

Defining the minimum height of the inline element span

The span tag is often used when making HTML web p...

Implementation of vscode custom vue template

Use the vscode editor to create a vue template, s...

Super detailed steps to install zabbix3.0 on centos7

Preface Recently, part of the company's busin...

A detailed discussion of components in Vue

Table of contents 1. Component Registration 2. Us...

How to quickly query 10 million records in Mysql

Table of contents Normal paging query How to opti...

How to handle spaces in CSS

1. Space rules Whitespace within HTML code is usu...