npm vs npx - What's the difference?

Yuta Hamasaki - Nov 1 - - Dev Community

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.

. . .
Terabox Video Player