NPX: An npm package runner
Posted By : Abhinav Gupta | 31-Mar-2019
The newer versions of npm (>= [email protected]) are being shipped with a binary `NPX` installed along with the usual npm: npx. The primary function of NPX is to round off and smoothen the process of exploring and using CLI tools and other executables hosted on the npm registry, just as npm smoothens the process of installing and managing dependencies hosted on the registry. NPX simplifies a number of things that used to be quite a hassle with plain npm.
Executing/Using locally installed tools
These days for mostly all node based projects, tools and dependencies are installed locally as devDependencies rather than requiring the user to install them globally i.e, tools like nodemon, gulp or yarn, which were at one time installed globally, now they allow for a project-local installation, and can have different version on per-project basis. This also allows for getting a node project up and running, even with a minimal node + npm setup. `npm run-script` adds the local binaries to the PATH, and it works okay
But the problem with this is, that there is no convenient way for a user to invoke the locally installed tool interactively. To call these tools interactively, the user would have to either add these local-binaries to scripts, and to pass any argument using -- or the user would have to call the binaries manually by entering the path like `./node_modules/.bin/nodemon`. These methods work but are far from convenient. Here NPX offers a convenient solution, where `NPX nodemon` is all a user would have to run, to execute the local installation of nodemon. NPX is smart enough to plug the code for the tool into the already running node process (if there is an already running node process).
Sampling new commands
For commands such as express-generator, which are used mostly once in the life of a project, NPX allows for a package to be temporarily installed and run, without installing it globally, and thus reducing the risk of pollution in the system. Some neat npm packages that can be tried with npx are listed here. Run these packages using `npx {commandName}`.
Request for Proposal
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Abhinav Gupta
Abhinav is good at developing commercial software that save time and money. He is experienced with node.js and javascript.