npx
is a tool for running npm
packages that:
- live inside of a local
node_modules
folder or - are not installed globally.
# Before
$ node ./node_modules/.bin/mocha
# Now with npx:
$ npx mocha
npx
looks into the local /node_modules
folder for the package and if it can’t
find it, it will download and run it without having that package globally installed.
npx can reach into node_modules
to run package binaries
npx
is a replacement for installing global packages. It encourages you to install packages
locally, but still be able run them as if they were global, just with npx
.
$ npm i -D gulp gulp-cli
$ npx gulp
Make sure you --save
or --save-dev
the package first.
This keeps dependent packages listed in package.json, so that npx
can use the local version instead of downloading it to the npm cache.
npx
is a useful tool for easily running npm
packages. Just be wary of typos as this method increases the chance of being
typosquatted.
npx comes with npm
npx
comes bundled with npm version 5.2+ (or as a standalone package). It works by checking if the npm package command exists in your local node_modules/.bin
folder, or from a central npx
cache and installing any packages needed for that command to run.
Run any one-off package
npx
will download and execute any package you give it.
This is useful for one-off commands, such as:
Checking the accessibility score of a website.
$ npx pa11y https://scottlogic.com
> Running Pa11y on URL https://scottlogic.com/
After performing this audit we have promptly looked into the accessibility of our website and how we can improve its Pa11y score.
Creating a boilerplate React app.
$ npx create-react-app harrys-site
Running a static web server.
$ cd my-website
$ npx http-server
> Starting up http-server, serving ./
> Available on:
> http://192.168.36.65:8080
> http://127.0.0.1:8080
> Hit CTRL-C to stop the server
Deploying to a live server.
$ cd my-website
$ npx now --public
> Ready! https://test-hffvgcpvvq.now.sh [2s]
npx
can also execute a package from a URL, e.g. this GitHub gist:
$ npx https://gist.github.com/zkat/4bc19503fe9e9309e2bfaa2c58074d32
> yay gist
Use it today by updating npm: npm i npm
.
$ npx cowsay goodbye!