Technology Sharing

Package Managers - Comparison of npm, yarn, cnpm, and pnpm

2024-07-12

한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina

1. npm (node package manage)

1.1 Local Installation

Use command: npm install package name or npm i package name
Locally installed packages appear in the node_module directory under the current directory
If the locally installed package comes with a CLI, npm will place its CLI script in node_modules/.bin and call it using the npx command.

1.2 Global Installation

Globally installed packages are placed in a special global directory.
Use the command npm install --global package name or npm i -g package name.
The globally installed package is not available for all projects. It only provides global CLI tools. In most cases, you don't need to install the package globally.

1.3 Installation Commands

Dependency on production environment
npm i package name
npm i --save package name
npm i -S package name
Install dependencies into the development environment
npm i --save-dev package name
npm i -D package name

1.4. When using nodejs to import a module, if the module path does not start with ./ or .../, node will assume that the imported module comes from the node_modules directory.
1.5. npm script

Configure common CLI commands in the script field of package.json, and run the script using npm run script name. npx can be omitted in the script.

2. Problems that npm had in the past:

  • Deep nesting of dependency directories: In the past, npm dependencies were nested, and the Windows system could not support directories that were too deep.
  • Slow download speed: Due to deep nesting levels, packages can only be downloaded serially; multiple packages of the same version are downloaded repeatedly.
  • The console output is complex: it prints detailed information about many packages
  • Project migration problem: Previously there was only a package.json configuration file, but no package-lock.json file

3. The emergence of yarn, because it has the following advantages:

  • Use a flat directory structure
  • Parallel downloads
  • Using local cache
  • Simplify console output information and only output key information
  • Use yarn-lock files to record exact dependencies

4. Influenced by yarn, npm6 draws on the advanced concepts of yarn and makes the following optimizations:

  • Flattening the catalog
  • Parallel downloads
  • Local Cache
  • Use package-lock.json to record exact dependencies
  • Added a lot of command aliases
  • Built-in npx, can start local CLI tools
  • Greatly simplified console output

5. cnpm

The npm registry server is located overseas, which may cause slow downloads or failures. In the past, npm did not provide the function of modifying the registry. Taobao built its own registry, namely the Taobao npm mirror. Taobao also provides a CLI tool cnpm, which is basically the same as npm in other uses.

6. pnpm

  • Like npm and yarn, it still uses cache to save installed packages. Use pnpm-lock.yaml to record detailed dependency versions
  • Unlike yarn and npm, pnpm uses symbolic links and hard links to place dependencies, avoiding file copying, improving installation efficiency, and greatly reducing disk space usage.
  • By using symbolic links and hard links, pnpm can avoid the problem of long paths in Windows systems, so it uses tree dependencies. Because of tree dependencies, projects can only use direct dependencies, not indirect dependencies.

7. pnpm principle:

  • The nature of files: A file is actually a pointer that points to an external storage address (hard disk, USB flash drive). Deleting a file actually deletes the pointer, so the speed is very fast.
  • File copy: copy the content pointed to by the file pointer, and then generate a new pointer to point to the new content.
  • Hard link: Copy a file A pointer to another file B pointer, and file B is a hard link to file A.
  • Symbolic link (soft link): Create a symbolic link B for a file or folder A, then B points to A.
  • Shortcut: Similar to a symbolic link, it is a link method supported by Windows in the early days. It is not only a pointer, but also contains various information such as permissions, compatibility, and startup methods. Shortcuts are unique to Windows and are not cross-platform.

Difference between symbolic links and hard links

  • Hard links can only link files, while symbolic links can connect files and directories.
  • After the hard link is connected, it is only associated with the file content and has nothing to do with the previous link. Symbolic links are always associated with the previously linked file and are not directly associated with the content file.