967

I have just started learning React, and Facebook helps in simplifying the initial setup by providing the following ready-made project.

If I have to install the skeleton project I have to type npx create-react-app my-app in command-line.

I was wondering why does the Facebook in Github have npx create-react-app my-app rather than npm create-react-app my-app?

Paresh Maniyar
  • 9,870
  • 3
  • 11
  • 14
  • 71
    `create-react-app` is a generator. **`npx`** goes to the *internet*, downloading the package temporarily so it could execute it (*np**x***). What you get (and want) is the output, which is saved locally, where you had run the command. – vsync Sep 23 '18 at 08:58
  • Related post - [How to use package installed locally in node_modules?](https://stackoverflow.com/q/9679932/465053) – RBT Jan 08 '19 at 11:37
  • 2
    This post explains why to use npx with an example https://thecodeframework.com/how-to-use-any-npm-package-without-installing-it/ – Gagan Jul 20 '20 at 04:00
  • This article explained it pretty well. https://www.section.io/engineering-education/npm-vs-npx/ – Zahid Jun 24 '22 at 15:50

25 Answers25

1135

Introducing npx: an npm package runner

NPM - Manages packages but doesn't make life easy executing any.
NPX - A tool for executing Node packages.

NPX comes bundled with NPM version 5.2+

NPM by itself does not simply run any package. It doesn't run any package as a matter of fact. If you want to run a package using NPM, you must specify that package in your package.json file.

When executables are installed via NPM packages, NPM links to them:

  1. local installs have "links" created at ./node_modules/.bin/ directory.
  2. global installs have "links" created from the global bin/ directory (e.g. /usr/local/bin) on Linux or at %AppData%/npm on Windows.

Documentation you should read


NPM:

One might install a package locally on a certain project:

npm install some-package

Now let's say you want NodeJS to execute that package from the command line:

$ some-package

The above will fail. Only globally installed packages can be executed by typing their name only.

To fix this, and have it run, you must type the local path:

$ ./node_modules/.bin/some-package

You can technically run a locally installed package by editing your packages.json file and adding that package in the scripts section:

{
  "name": "whatever",
  "version": "1.0.0",
  "scripts": {
    "some-package": "some-package"
  }
}

Then run the script using npm run-script (or npm run):

npm run some-package

NPX:

npx will check whether <command> exists in $PATH, or in the local project binaries, and execute it. So, for the above example, if you wish to execute the locally-installed package some-package all you need to do is type:

npx some-package

Another major advantage of npx is the ability to execute a package which wasn't previously installed:

$ npx create-react-app my-app

The above example will generate a react app boilerplate within the path the command had run in, and ensures that you always use the latest version of a generator or build tool without having to upgrade each time you’re about to use it.


Use-Case Example:

npx command may be helpful in the script section of a package.json file, when it is unwanted to define a dependency which might not be commonly used or any other reason:

"scripts": {
    "start": "npx gulp@3.9.1",
    "serve": "npx http-server"
}

Call with: npm run serve


Related questions:

  1. How to use package installed locally in node_modules?
  2. NPM: how to source ./node_modules/.bin folder?
  3. How do you run a js file using npm scripts?
nCardot
  • 5,992
  • 6
  • 47
  • 83
vsync
  • 118,978
  • 58
  • 307
  • 400
  • 4
    So react doens't use `node.js`, right? Why is it available through 'npm' (the node package manager)? – winklerrr Mar 27 '19 at 14:57
  • 7
    Some years ago, there was [Bower](https://bower.io/) for frontend package management, but it's use drastically fell in favour of npm. Among some reasons, you could just use a single package manager for everything related to javascript, and you can use commonjs resolutions for developing frontend projects as well. You can check [this response](https://stackoverflow.com/questions/18641899) for further details. Regarding react: no, it does not directly use node, although you can use it in node too! (e.g: server side rendering) – RecuencoJones Apr 04 '19 at 15:37
  • 1
    @RecuencoJones - You commented to my answer instead of OP – vsync Apr 04 '19 at 17:35
  • 1
    Yes, I forgot to mention @winklerrr, I was addressing his comment – RecuencoJones Apr 05 '19 at 06:31
  • 2
    I think the most useful feature is NPX installing the package when it has not yet been installed. Otherwise add `./node_modules/.bin` to your $PATH and NPX is not needed. – Ron E Oct 06 '19 at 14:25
  • 1
    If you are npx-ing package multiple times, there is a sense to install it either locally or globally. Because every time you execute a package via npx, it gets installed run and removed. A comment from npx owner: https://github.com/zkat/npx/issues/113#issuecomment-329848019 – mashi May 05 '20 at 01:36
  • what does "ensures that you always use the latest version of a generator or build tool without having to upgrade each time you're about to use it" mean? – utkarsh-k Jul 25 '21 at 17:48
143

npx is a npm package runner (x probably stands for eXecute). One common way to use npx is to download and run a package temporarily or for trials.

create-react-app is an npm package that is expected to be run only once in a project's lifecycle. Hence, it is preferred to use npx to install and run it in a single step.

As mentioned in the main page https://www.npmjs.com/package/npx, npx can run commands in the PATH or from node_modules/.bin by default.

Note: With some digging, we can find that create-react-app points to a Javascript file (possibly to /usr/lib/node_modules/create-react-app/index.js on Linux systems) that is executed within the node environment. This is simply a global tool that does some checks. The actual setup is done by react-scripts, whose latest version is installed in the project. Refer https://github.com/facebook/create-react-app for more info.

Karthick
  • 2,031
  • 1
  • 13
  • 18
  • 5
    The typical use is just as often to run the version already installed locally or globally as it is to download and run a package. –  Jul 28 '18 at 03:57
119

NPM is a package manager, you can install node.js packages using NPM

NPX is a tool to execute node.js packages.

It doesn't matter whether you installed that package globally or locally. NPX will temporarily install it and run it. NPM also can run packages if you configure a package.json file and include it in the script section.

So remember this, if you want to check/run a node package quickly without installing locally or globally use NPX.

npM - Manager

npX - Execute - easy to remember

cherankrish
  • 2,004
  • 1
  • 16
  • 11
  • 10
    Can you elaborate a bit on the "temporarily". Do you mean the binaries are thrown away after executing the command, forcing npx to re-download the binaries *every* time? – Jim Aho May 15 '20 at 20:38
  • 3
    @JimAho Yes. npx will re-download the package and its dependencies every time. If performance is a factor, you might want to install the package and execute it with something other than npx. (ex.: npm run my-package) – Émile Perron Aug 01 '20 at 21:56
  • I think "temporarily" means the author of the package does not want users to know where the package is installed and then re-use it again and again, because the package might need to be upgraded during the night... – themefield Jan 01 '21 at 05:44
  • 1
    "It doesn't matter whether you installed that package globally or locally. NPX will temporarily install it and run it." I cannot reproduce on `npx` 6.14.11 e.g. in `npm install --save vaca;npx vaca`, npx does not redownload: https://stackoverflow.com/questions/49302438/why-does-npx-install-webpack-every-time#comment118815415_49302438 – Ciro Santilli OurBigBook.com Apr 23 '21 at 19:40
  • "_doesn't matter whether you installed that package globally or locally. NPX will temporarily install it and run it._" Not sure that's 100% accurate. From [the npm docs](https://docs.npmjs.com/cli/v7/commands/npx): "_This command allows you to run an arbitrary command from an npm package (either one installed locally, or fetched remotely), in a similar context as running it via npm run. Whatever packages are specified by the --package option will be provided in the PATH of the executed command, along with any locally installed package executables._" If not found, fresh download. If found, not? – ruffin Jul 06 '23 at 00:47
65

npm - Package manager for JavaScript, just like: pip (Python), Maven (Java), NuGet (.NET), Composer (PHP), RubyGems (Ruby), ...

npx - runs a command of a package without installing it explicitly.

Use cases:

  • You don't want to install packages neither globally nor locally.
  • You don't have permission to install it globally.
  • Just want to test some commands.
  • Sometime, you want to have a script command (generate, convert something, ...) in package.json to execute something without installing these packages as project's dependencies.

Syntax:

npx [options] [-p|--package <package>] <command> [command-arg]...

Package is optional:

npx   -p uglify-js         uglifyjs --output app.min.js app.js common.js
      +----------------+   +--------------------------------------------+
      package (optional)   command, followed by arguments

For example:

Start a HTTP Server      : npx http-server
Lint code                : npx eslint ./src
                         # Run uglifyjs command in the package uglify-js
Minify JS                : npx -p uglify-js uglifyjs -o app.min.js app.js common.js
Minify CSS               : npx clean-css-cli -o style.min.css css/bootstrap.css style.css
Minify HTML              : npx html-minifier index-2.html -o index.html --remove-comments --collapse-whitespace
Scan for open ports      : npx evilscan 192.168.1.10 --port=10-9999
Cast video to Chromecast : npx castnow http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/ForBiggerFun.mp4

More about command:

ninhjs.dev
  • 7,203
  • 1
  • 49
  • 35
52

NPX:

From https://www.futurehosting.com/blog/npx-makes-life-easier-for-node-developers-plus-node-vulnerability-news/:

Web developers can have dozens of projects on their development machines, and each project has its own particular set of npm-installed dependencies. A few years back, the usual advice for dealing with CLI applications like Grunt or Gulp was to install them locally in each project and also globally so they could easily be run from the command line.

But installing globally caused as many problems as it solved. Projects may depend on different versions of command line tools, and polluting the operating system with lots of development-specific CLI tools isn’t great either. Today, most developers prefer to install tools locally and leave it at that.

Local versions of tools allow developers to pull projects from GitHub without worrying about incompatibilities with globally installed versions of tools. NPM can just install local versions and you’re good to go. But project specific installations aren’t without their problems: how do you run the right version of the tool without specifying its exact location in the project or playing around with aliases?

That’s the problem npx solves. A new tool included in NPM 5.2, npx is a small utility that’s smart enough to run the right application when it’s called from within a project.

If you wanted to run the project-local version of mocha, for example, you can run npx mocha inside the project and it will do what you expect.

A useful side benefit of npx is that it will automatically install npm packages that aren’t already installed. So, as the tool’s creator Kat Marchán points out, you can run npx benny-hill without having to deal with Benny Hill polluting the global environment.

If you want to take npx for a spin, update to the most recent version of npm.

nCardot
  • 5,992
  • 6
  • 47
  • 83
Venkat Ch
  • 696
  • 1
  • 5
  • 16
  • 1
    If you are using nvm-windows you likely won't get npx with npm, but need to install it manually! npm i -g npx – Neil Aug 20 '18 at 08:37
  • 2
    *`NPM can just install local versions`* - not correct. `npm` can install global and it's a common practice. – vsync Aug 25 '18 at 15:00
  • 1
    A great introduction to NPX can be found here: https://medium.com/@maybekatz/introducing-npx-an-npm-package-runner-55f7d4bd282b, by Kat Marchán. – Jeff Hu Sep 05 '18 at 00:41
  • 1
    @vsync I believe this should be interpreted as "NPM can _simply_ install local versions and you're good to go." rather than implying a limitation. – YipYip Nov 07 '18 at 23:29
24

Simple Definition:

npm - Javascript package manager

npx - Execute npm package binaries

Yassine Qoraiche
  • 1,077
  • 14
  • 32
13

Here's an example of NPX in action: npx cowsay hello

If you type that into your bash terminal you'll see the result. The benefit of this is that npx has temporarily installed cowsay. There is no package pollution since cowsay is not permanently installed. This is great for one off packages where you want to avoid package pollution.

As mentioned in other answers, npx is also very useful in cases where (with npm) the package needs to be installed then configured before running. E.g. instead of using npm to install and then configure the json.package file and then call the configured run command just use npx instead. A real example: npx create-react-app my-app

Risteard
  • 318
  • 2
  • 12
  • 3
    Where does it install it, and does it remove it after the command finishes, or does it cache it for some length of time, or always check that you have the most recent version of the package? – redOctober13 Jul 24 '19 at 13:08
  • 1
    @redOctober13 there's some caching, but npx also asks npm registry for newest version every time, so caching doesn't help too much with speed. And it expands and then cleans up dependencies every time. – Simon B. May 11 '20 at 12:55
12

NPM => Is a JS package manager.

NPX => Is a tool for executing Node packages and execute npm package binaries.

It is easy to remember:

-npm stands for MANAGER

-npx stands for EXECUTE

Chris
  • 806
  • 1
  • 10
  • 17
9

NPM: NPM stands for Node Package Manager and is the default package manager for Node.js. It was developed by Isaac Z. Schlueter and was originally released on January 12, 2010. It is entirely written in JavaScript. It consists of a command-line client npm which manages all node.js packages and modules. When node.js is installed, it is included in the installation.

npm run your-package-name

NPX is a tool that use to execute packages.

NPX is an acronym for Node Package Execute The NPX package comes with npm, so when you install npm above 5.2.0, NPX will be installed automatically.

It is an npm package runner that can execute any package that you want from the npm registry without even installing that package. The npx is useful during a single time use package. If you have installed npm below 5.2.0 then npx is not installed in your system.

Run the following command to determine if npx is installed:

npx -v

The following command can be run if npx is not installed.

npm install -g npx

Use npx to execute the package:

npx your-package-name

NPM VS NPX

Midhun Pottammal
  • 1,107
  • 1
  • 11
  • 25
4

NPM stands for Node Package Manager. NPM is Node.JS's default package manager. It's written in Javascript. The role of NPM is to manage the package and modules of node.js.

NPX stands for Node Package Execute. NPX comes with npm, when npm is installed above the 5.2.0 version, it gets installed automatically. NPX is an npm package runner and its role is to execute the package from the registry without even installing that package.

Now, the differences between NPM and NPX are as below:

i) NPM is used to install the packages while NPX is used to execute the packages.

ii) Due to npm the packages installed have to be taken care of since it's installed globally while the packages which are used by npx don't need to be taken care of as they are not installed globally.

Manishyadav
  • 1,361
  • 1
  • 8
  • 26
3

Simplest Definition:

NPX

The npx stands for Node Package Execute and it comes with the npm, when you installed npm above 5.2.0 version then automatically npx will installed. It is an npm package runner that can execute any package that you want from the npm registry without even installing that package.

NPM

npm is a package manager for the JavaScript programming language maintained by npm, Inc. npm is the default package manager for the JavaScript runtime environment Node.js. It consists of a command line client, also called npm, and an online database of public and paid-for private packages

3

NPM vs. NPX

NPM stands for the Node Package Manager. A text based program for Nodejs package management.

While NPX is a Node Package Runner. Its function is to execute the Nodejs package

NPX will execute binary files from the Nodejs package, both installed and not.

Even NPX can also help us use certain versions of Nodejs without having to use nvm (node.js version management), nave (node.js virtual environment), and nvm (node.js version management).

2

Simple answer is like

NPX: is used to execute any node package without installing the package on our machine.

NPM: is used to install any node js package in our machine. We can use "require("package-name')" when we install any package using NPM. but we can not import the package when we use NPX.

Example: You should run npm i axios in this case you are installing axios package in your local machine

and npx create-react-app 'app-name' here you are executing the create-react-app package directly on your machine without installing it's files.

Shanu Raj
  • 95
  • 1
  • 8
1

NPM stands for Node Package Manager. It comes pre-installed with Node.js. NPM helps to manage packages in your projects as dependencies.

When using NPM, there are two ways to install a package into your local computer.

  1. Locally: When a package is installed locally, it is installed in ./node_modules/.bin/ of the local project directory.

  2. Globally: A global package is installed in the user environment path. /usr/local/bin for Linux and AppData%/npm for Windows.

To execute a locally installed package, it should be specified in the package.json scripts block as shown below.

"scripts": {
    "your-package":  "your-package-name"
}

Then, you can execute the package with:

npm run your-package-name

NPX is an NPM package executor. Currently, NPX is bundled with NPM when you install the NPM version 5.2.0 or higher.

Why NPX over NPM?

  • No need to edit the package.json file with node_modules paths.

  • You can directly execute the tool from the command line.

Sunil Yadav
  • 17
  • 1
  • 6
1

The differences between NPM and NPX are as below:

i) NPM is used to install the packages while NPX is used to execute the packages.

ii) Due to npm the packages installed have to be taken care of since it's installed globally while the packages used by npx don't need to be taken care of as they are not installed globally.

Ashim
  • 11
  • 1
1

NPX is a tool for creating and executing some features in a new project NPM is the package manager that contains all of libraries

Ahad
  • 49
  • 1
  • 6
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 27 '22 at 14:50
1

Here is the simple definition. NPM is a package manager, you can install node.js packages using NPM

NPX is a tool to execute node.js packages.

1

with npx:

npx create-react-app MyApp

earlier:

npm install -g create-react-app
create-react-app MyApp

Earlier it was necessary to install a CLI tool locally before using it, with npx that is not required anymore.

0

Here's an example of what your app creation might look like using npx

npx create-react-app project-name --template all

Charney Kaye
  • 3,667
  • 6
  • 41
  • 54
Chaurasia
  • 494
  • 1
  • 6
  • 22
0

Simply npm is the Node Package Manager and npx is the executeable version that run npm packages

Sajol
  • 11
  • 3
0

npm is a tool that use to install packages and npx is a tool that use to execute packages. npm-If you wish to run package through npm then you have to specify that package in your package.json and install it locally. npx-A package can be executable without installing the package. It is an npm package runner so if any packages aren’t already installed it will install them automatically.

0

npm - package manager

npx - Execute npm package

This is a difference with it.

0

NPM - NODE PACKAGE MANAGER

  1. it's a package manager.

  2. node.js package can be installed using NPM

NPX - NODE PACKAGE EXECUTE

  1. it execute the node.js package

  2. doesn't matter whether you installed node.js package locally or globally.

  3. NPX will temporarily install package and run it.

  4. NPX is used when you want to execute package for single time.

.

-1

npm is package manager or installer on the other hand Packages used by npx are not installed globally so you have to carefree for the pollution for the long term.

-4

Actually, I tried many ways to solve this and failed but finally removing/deleting yarn globally solves the problem

just type this command in your commandline terminal:

npm uninstall -g yarn

And then run the command below to install the react starter project

npx create-react-app