Ir al contenido

Dependency Management and PNPM

Best practices in package management, installation of native dependencies, and why PNPM is infinitely superior and more secure than NPM in React Native.

0views

This guide comprehensively addresses the correct methodologies for the installation, update, and integration of dependencies in React Native projects using PNPM, while also detailing why it is the optimal choice compared to the traditional NPM package manager.


Historically, NPM has carried serious architectural problems that directly affect the security, stability, and performance of development projects:

  1. Phantom Dependencies:
    • The Problem: NPM uses a flat dependency tree (flat node_modules). If you install the A package, and it internally requires the B package, NPM places B directly at the root of node_modules.
    • The Security Risk: Your code can import from B directly without formally declaring it in your package.json. If you update A in the future and it no longer depends on B, your code will break in production unexpectedly. Additionally, it opens the door to supply chain attacks where malicious code inadvertently accesses undeclared flat packages.
  2. File Duplication and Disk Locking:
    • NPM downloads redundant physical copies of the same packages in every local project on your machine, filling your hard drive with gigabytes of unnecessary code.
  3. Excessive Slowness:
    • The flat tree resolution process requires re-calculating and indexing thousands of folders in each installation, slowing down your continuous integration and continuous deployment (CI/CD) pipelines.

The PNPM Solution: Innovation in Structure and Security

Sección titulada «La Solución de PNPM: Innovación en Estructura y Seguridad»

PNPM (Performant NPM) solves these problems through a completely revolutionary approach:

graph TD
A[Almacén Central de PNPM en Disco] -->|Enlace Físico / Hard Link| B["mi-proyecto/node_modules/.pnpm"]
B -->|Enlaces Simbólicos / Symlinks| C["Dependencias Declaradas strictly en package.json"]
  • Non-Flat node_modules Structure:
    • PNPM creates symbolic links (symlinks). Only packages explicitly declared in your package.json are accessible at the root of node_modules. It is physically impossible to import undeclared phantom packages, nullifying this attack vector.
  • Content-Addressable Storage (Global Store):
    • All packages are downloaded only once to a global system directory. Hard links (hard links) are created in your projects pointing to that single store. You save up to 90% of disk space!
  • Superior Speed:
    • Since it does not physically copy files and reads from the global cache, installations with PNPM are up to 3x faster than with NPM.

FeatureNPMYarn (Classic)PNPM
Phantom Dependency Prevention✕ No✕ No✓ Yes (By design)
Efficient Disk Space✕ No (Physical copy)✕ No (Physical copy)✓ Yes (Global Hard Links)
Installation SpeedSlowMediumUltra Fast
Link SecurityLow (Flat tree)Low (Flat tree)High (Strict structure)

Since React Native contains compiled code for native mobile platforms, module installation must be done in a coordinated manner:

Recommended Command

For any package containing native modules, always use pnpm expo install instead of raw installation commands.

When running pnpm expo install <paquete>:

  1. Expo verifies the exact certified version compatible with your SDK's active version.
  2. PNPM performs an ultra-fast installation by safely resolving the physical linking structure in node_modules/.pnpm/.
Ventana de terminal
# Instalar iconos vectoriales y almacenamiento asíncrono nativo de forma segura
pnpm expo install lucide-react-native @react-native-async-storage/async-storage

Once packages are installed with PNPM, integration with the native Gradle (Android) and CocoaPods (iOS) folders runs naturally:

Ventana de terminal
# Compilar binarios de desarrollo nativos integrando las dependencias instaladas
pnpm expo run:android
pnpm expo run:ios
  • pnpm dlx expo-doctor: Diagnoses and validates the overall health of the SDK and the project's native dependencies.
  • pnpm expo install --fix: Automatically and immediately resolves any misaligned version in your dependency tree, forcing it to align with the recommended Expo release.