npx, npm, and yarn

shubham patidarshubham patidar
4 min read

Here's a breakdown of the differences between npx, npm, and yarn:

1. npm (Node Package Manager)

  • What is npm?

    • npm is the default package manager for JavaScript and is bundled with Node.js.

    • It allows you to manage dependencies in your project, install packages, publish packages, and run scripts.

  • Common commands:

    • npm install <package-name>: Install a package locally in the project.

    • npm install -g <package-name>: Install a package globally.

    • npm run <script>: Run a script defined in your package.json.

  • Key features:

    • Dependency management: npm installs packages locally by default in your project's node_modules directory.

    • Script execution: You can define and run custom scripts via the scripts section of package.json.

    • Central repository: npm is the largest repository for JavaScript packages.

2. npx (Node Package eXecute)

  • What is npx?

    • npx is a package runner tool that comes with npm (since version 5.2). It allows you to execute packages directly without needing to install them globally or locally in your project.
  • Common use cases:

    • Run packages without installation.

    • npx create-react-app my-app

    • Run one-off commands without adding the package as a dependency in your project.

    • Useful for running scripts or binaries like nodemon or webpack without the need for global installation.

  • Key features:

    • Avoids global installs: You don't need to globally install packages to run them.

    • Temporary usage: The package is downloaded and executed, and then it’s removed.

    • Version control: You can specify the exact version of a package you want to run.

    • npx create-react-app@3.0.0 my-app

  • Why use npx?

    • To avoid "global clutter" by running tools without globally installing them.

    • It's ideal for short-term or one-time use of tools like create-react-app, typescript, etc.

3. Yarn

  • What is Yarn?

    • Yarn is an alternative package manager developed by Facebook to solve some issues with npm (such as performance and security).

    • It is compatible with npm's package registry but has its own distinct features and speed improvements.

  • Common commands:

    • yarn add <package-name>: Install a package and add it to package.json.

    • yarn global add <package-name>: Install a package globally.

    • yarn run <script>: Run a script defined in your package.json.

  • Key features:

    • Speed: Yarn uses parallel installation and cache, which makes it faster than npm in many scenarios.

    • Offline installation: If you’ve installed a package before, Yarn can install it offline using its cache.

    • Deterministic dependency resolution: Yarn generates a yarn.lock file (similar to package-lock.json in npm), ensuring that the same dependency tree is installed every time.

    • Workspaces: Yarn has built-in support for managing multiple packages in a monorepo (projects with multiple sub-packages).

  • Differences from npm:

    • Speed: Yarn is often faster than npm due to its caching and parallel installation features.

    • Security: Yarn verifies package integrity via checksums.

    • Lock files: Both npm (package-lock.json) and Yarn (yarn.lock) generate lock files, but Yarn was earlier to adopt this.


Comparison Summary

FeaturenpmnpxYarn
PurposeInstall/manage packagesRun packages directly without installingAlternative package manager for npm
InstallationInstalls packages globally or locallyExecutes a package without installationInstalls packages locally/globally
PerformanceSlower compared to YarnN/A (since it’s temporary)Faster, with caching and parallel installs
Lock filepackage-lock.jsonN/Ayarn.lock
Offline modeLimitedN/AAvailable with cache
Monorepo supportLimitedN/ABuilt-in
UsageMost common for dependency managementRunning packages like create-react-appFaster package installs and dependency management

When to use what?

  • npm: Use npm when you’re managing dependencies, scripts, or publishing packages.

  • npx: Use npx when you want to quickly run a package without installing it globally, such as create-react-app, nodemon, or any package executable.

  • Yarn: Use Yarn when you need faster installs, better caching, or workspaces (for managing multiple projects in a monorepo). It’s great for projects with more complex dependency trees.

Happy coding! Thank you for reading, keep learning and building amazing things! 😊

1
Subscribe to my newsletter

Read articles from shubham patidar directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

shubham patidar
shubham patidar