DEV Community

Cover image for npm vs npx: whats the difference?
Francisco Juan
Francisco Juan

Posted on

npm vs npx: whats the difference?

Hello there!

If you are starting on Node.js ecossistem its very usually confusing with the differences understanding npm and npx. What do they? What the diffence? Where do they live?

npm

The npm is the package manager of node, (Node Package Manager).

You use npm maining for install dependences and run scripts of project, those one writed into manifest package.json

What is a manifest?

For example:

npm install react # Installing a normal package
npm install -D eslint # Installing a developer package
npm run dev # Running a dev script from `package.json`
Enter fullscreen mode Exit fullscreen mode

In the end, the npm do this sequence:

  • download the package
  • add it into node_modules
  • register into package.json

npx

The npx was made for execution of packages, with no need of a global installation.

Examples:

npx create-react-app my-app
npx eslint .
npx prisma migrate dev
Enter fullscreen mode Exit fullscreen mode

For curious devs, you already must seen the .bin folder into node_modules, if you open that, you can see the execution binaries of your package, usually it be of packages that you can run on your CLI.

On running npx, it:

  • use local version of package (if it exists)
  • or download the package temporarily, execute, and discard

It's like, getting of example create-react-app, the npx download temporarily the create-react-app package, runs it, without installing global avoiding the risks of conflicts with other global versions.

Now, when you run inside of your working directory, and you already have this package installed into your project scope, the npx only runs it, like the prisma and eslint into examples.

Difference in the practice

  • npm → install and manage dependences
  • npx → run package binary

A simple rule to remember is:

npm install and npx runs

Since npx, make no sense install tools globally.

If this post help you, tell us more examples, your uses, etc.

very thank you!

a main()

Top comments (0)