npx – beyond the basics

If you’ve worked with Node.js, you’ve probably come across npm commands like install and run. But have you ever wondered about npx — what it does, how it works behind the scenes, and how to manage the temporary packages it downloads? In this post, we’ll explore npx, global packages, and best practices for keeping your Node environment clean.

What is npx?

npx is a package runner that comes bundled with npm v5.2.0+. Its main purpose?

Run Node.js packages without installing them globally.

This makes it perfect for one-off commands, CLI tools, and scripts without cluttering your global environment.

How npx Works

When you run a command like:

npx create-react-app my-app

Here’s what happens:

  1. Check local project binaries npx first looks in node_modules/.bin of your project. If the package exists locally, it runs it.
  2. Download if not found If not found locally, npx downloads a temporary copy from npm. This is stored in a cache folder:
    • macOS/Linux: ~/.npm/_npx/
    • Windows: C:\Users\<username>\AppData\Roaming\npm-cache\_npx\
  3. Run the package npx temporarily adds the package to your PATH and executes it.
  4. Cleanup Temporary folders may be deleted immediately after execution, though cached versions are often retained for reuse.

Examples of npx in Action

Run a one-off CLI tool:

npx cowsay "Hello, Vinod!"

Run a specific package version:

npx create-react-app@5.0.1 my-app

Use local binaries without installing globally:

npx eslint .

Run scripts from GitHub:

npx github:username/repo

Pro Tip: Add --verbose to see what npx is doing under the hood:

npx --verbose cowsay "Hi Vinod"

Managing Globally Installed Packages

Sometimes you might want to see or clean up your global Node packages.

List All Globally Installed Packages

npm list -g --depth=0

Example output:

/usr/local/lib
├── npm@10.2.4
├── typescript@5.4.3
├── nodemon@3.0.2
└── eslint@9.1.0

Find Where Global Packages and Executables Live

  • Global package root:
npm root -g
  • Global binaries (CLI commands):
npm bin -g
  • Check exact executable path:
which nodemon   # Linux/macOS
where nodemon   # Windows

Uninstall a Global Package Safely

npm uninstall -g nodemon

After uninstalling, verify removal:

npm list -g --depth=0
which nodemon

⚠️ Tip: Avoid installing tools globally when possible. Use npx for one-off runs to keep your environment clean.

Cleaning Up Temporary npx Packages

npx downloads temporary packages to your npm cache. Over time, these can accumulate and take space.

Locate the Cache

npm config get cache

Typical location:

  • macOS/Linux: ~/.npm/_npx/
  • Windows: C:\Users\<username>\AppData\Roaming\npm-cache\_npx\

Manual Cleanup

macOS/Linux:

rm -rf ~/.npm/_npx

Windows (PowerShell):

Remove-Item "$env:AppData\npm-cache\_npx" -Recurse -Force

Using npm to Clean Cache

npm cache clean --force

⚠️ This removes all npm cached packages, not just npx temporary files. Use carefully.

Preventing Unnecessary Downloads

If you already have a local version of a package, you can avoid downloading:

npx --no-install eslint .

Summary

  • npx lets you run packages without global installs.
  • Temporary packages live in the npm cache and can be cleaned manually or with npm cache clean.
  • Check global packages with npm list -g --depth=0, and uninstall safely with npm uninstall -g <package>.
  • Use npx --no-install to avoid unnecessary downloads.

By understanding npx and global package management, you can keep your Node.js environment clean, efficient, and up-to-date.

Leave a Comment

Your email address will not be published. Required fields are marked *