DEV Community

Yuta Hamasaki
Yuta Hamasaki

Posted on

2 1

npm vs npx - What's the difference?

I had been using npm and npx without knowing the difference, but after the lecture where my instructor explained them, I’ve made a quick summary. Understanding what npm and npx are helps to use them effectively.

Contents

  • What is npm?
  • What is npx?
  • Summary

What is npm?

npm stands for Node Package Manager and is a system for managing Node.js packages. It installs, updates, and removes packages as you develop your project.。

Example usage:
To install a package:


npm install ・・・
Enter fullscreen mode Exit fullscreen mode

To uninstall a package:

npm uninstall ・・・
Enter fullscreen mode Exit fullscreen mode

To update a package:

npm update ・・・
Enter fullscreen mode Exit fullscreen mode

When executing scripts, use npm to run tasks specified in package.json:

npm run ・・・
Enter fullscreen mode Exit fullscreen mode

Examples: Commands like npm run dev or npm run build.

What is npx?

npx stands for Node Package Execute, a tool for running packages. It can temporarily install and execute a package that isn’t already installed, and it removes the package afterward.

In addition to temporary use, npx can also directly execute installed packages, such as npx ts-node or npx nodemon. This improves convenience in the command line by making tools directly accessible.

Example usage:

npx ・・・
Enter fullscreen mode Exit fullscreen mode

For instance, when running npx create-next-app, npx installs create-next-apptemporarily for immediate use and then removes it when finished.

Summary

For packages you plan to use long-term, use npm install to add them to your project dependencies. For one-time commands or temporary packages, use npx, which provides the convenience of automatic cleanup.

This approach minimizes project dependencies, ensuring efficient project management and command execution.

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read full post →

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more