NPX is a NPM package runner, which means it makes it easy to use command line (CLI) and other executable tools from the npm registry.
What’s the difference between npm and npx?
- npm installs the packages from the registry.
- npx helps with executing the node package.
How to install npx?
npx is included in npm > 5.2.0 as an extra binary, if you have npm installed you already have npx on your system.
To check if npx is installed, you can run this command:
which npx
If for some reason, you get npx command not working
or npx command not found
error message, you can manually install npm globally with the following command:
npm i -g npx
How to run npx?
The way to run npx is as follows:
npx < command >
Do I need to install < command > module in my $PATH before running npx < command >?
No, npx is smart enough in that if it does not find in your $PATH it will install an npm package from the registry with the same name as < command > and invoke it. Once the command has run the new package will be removed from your globals so you don’t have to worry about pollution on the long term.
What are some examples of using npx?
- Check for outdated, incorrect, and unused dependencies: npx npm-check
- Generate a react app boilerplate: npx create-react-app my_app
- Get a happy birthday message in different languages: npx happy-birthday -u John
- Make people believe you are working hard: npx workin-hard
How to execute local packages without using npx?
npm install package-to-execute
$ ./node_modules/.bin/package-to-execute
Can I execute packages by only using their name without using npx?
Yes, but only if the packages are installed globally.
For example:
$ npm install -g happy-birthday
$ happy-birthday -u John
?
Grattis på födelsedagen, John!
?
$ npm uninstall -g happy-birthday
Locally installed packages will only work to be executed by name if
you edit the package.json file and add the package to the scripts like the following:
{
"name": "test_app",
"version": "1.0.0",
"scripts": {
"package-to-execute": "package-to-execute"
}
}
After that use the npm run package-to-execute.
Can I use npx with a version of npm < 5.2.0?
Yes, but you have to install npx globally.
npm install -g npx