gotchajavascriptreactCritical
Difference between npx and npm?
Viewed 0 times
npxandbetweennpmdifference
Problem
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
I was wondering why does the Facebook in Github have
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?Solution
Introducing npx: an npm package runner
When executables are installed via NPM packages, NPM links to them:
Documentation you should read
NPM:
One might install a package locally on a certain project:
Now let's say you want NodeJS to execute that package from the command line:
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:
You can technically run a locally installed package by editing your
Then run the script using
NPX:
"start": "npx gulp@3.9.1",
"serve": "npx http-server"
}
Related questions:
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:
- local installs have "links" created at
./node_modules/.bin/directory.
- global installs have "links" created from the global
bin/directory (e.g./usr/local/bin) on Linux or at%AppData%/npmon Windows.
Documentation you should read
NPM:
One might install a package locally on a certain project:
npm install some-packageNow let's say you want NodeJS to execute that package from the command line:
$ some-packageThe 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-packageYou 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-packageNPX:
npx will check whether ` 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:
- How to use package installed locally in node_modules?
- NPM: how to source ./node_modules/.bin folder?
- How do you run a js file using npm scripts?
Code Snippets
npm install some-package$ some-package$ ./node_modules/.bin/some-package{
"name": "whatever",
"version": "1.0.0",
"scripts": {
"some-package": "some-package"
}
}npm run some-packageContext
Stack Overflow Q#50605219, score: 1278
Revisions (0)
No revisions yet.