npx is the npm package executor. npx checks if an npm command exists in $PATH, or in the local project binaries, and then executes the command. If the command is not found, then the command will be installed using npm prior to its execution.
npx
comes with npm
installation v5.2.0 or later
# Stand-alone version of `npx`
npm install -g npx
npx
is a tool for executing Node packages.
With npm
, it is not possible to run any Node package without a run-script defined in package.json file.
# package.json
"scripts": {
"build": "gatsby build",
"develop": "gatsby develop",
}
...
$ cd project_directory/
$ npm run build
However, with npx
, it is possible to run any Node package
npx gatsby build
You can run a Node package which was previously not installed on your machine.
npx create-react-app my-app
npx
uses npm
to install the Node packages into ~/.npm/_npx and adds them temporarily to $PATH
npm uninstall -g create-react-app
removed 114 packages in 2.233s
npx create-react-app task-manager
npx: installed 63 in 4.758s
Creating a new React app in /Users/klakum/Documents/Projects/task-manager.
Installing packages. This might take a couple of minutes.
Installing react, react-dom, and react-scripts...
...
Happy hacking!
While adding boilerplate for task-manager, you can actually see the create-react-app
binary file cached at ~/.npm/_npx.
This binary file and associated files are removed once npx
completes its execution.
ls -alih ~/.npm/_npx/
ls ~/.npm/_npx/52147/bin/create-react-app
/Users/klakum/.npm/_npx/52147/bin/create-react-app
You can run Node commands using different versions of the Node without installing them on your machine
npx node@v8.9.4 -v
v8.9.4
You can install npm dependencies for your project using a particular version of Node to verify backward compatibility of libraries
npx -p node@6 npm it
You can run npm scripts (defined in your project’s package.json) with specific Node version
npx -p node@8 npm run build
With npx, you will no longer need to install and use a Node version manager (nvm)
Examples
npx happy-birthday -u "Yaswin"
npx: installed 2 in 4.332s
🎂
పుట్టినరోజు శుభాకాంక్షలు Yaswin!
🎉
npx cowsay hello
npx: installed 10 in 9.553s
_______
< hello >
-------
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||
npx http-server
npx: installed 25 in 11.095s
Starting up http-server, serving ./
Available on:
http://127.0.0.1:8080
http://192.168.0.104:8080
Hit CTRL-C to stop the server
npx npm-check --skip-unused
npx: installed 247 in 9.048s
@uirouter/angularjs 😎 PATCH UP Patch update available. https://ui-router.github.io
npm install --save @uirouter/angularjs@1.0.20 to go from 1.0.13 to 1.0.20
angular 😎 MINOR UP Minor update available. http://angularjs.org
npm install --save angular@1.7.5 to go from 1.5.8 to 1.7.5
...
Use npm-check -u for interactive update.