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
?
Npm and Yarn are the comparison and Yarn integrates what npm calls npx into its command without needing to call a different command. All npx does is add the . bin folder to your path before searching for a command to execute, in essence.
TABLE OF CONTENTS. npx is a very powerful command that's been available in npm starting version 5.2, released in July 2017.
npx on the first line is not a typo — it's a package runner tool that comes with npm 5.2+. Create React App doesn't handle backend logic or databases; it just creates a frontend build pipeline, so you can use it with any backend you want.
Here is some common comparison summary between NPX and NPM. It is a Node. js package manager. Used to install, update, and manages package dependencies.
NPM
- Manages packages but doesn't make life easy executing any.NPX
- A tool for executing Node packages.
NPX
comes bundled withNPM
version5.2+
NPM
by itself does not simply run any package. it doesn't run any package in 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:
./node_modules/.bin/
directory.bin/
directory (e.g. /usr/local/bin
) on Linux or at %AppData%/npm
on Windows.Documentation you should read
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
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.
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 [email protected]", "serve": "npx http-server" }
Call with: npm run serve
npx is a npm package runner (x probably stands for eXecute). The typical use 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With