With Node.js, building CLI utilities and development tools have gotten so much easier.
Though, it also means that you need to install the CLI package on your computer globally, to use/execute that package as a regular bash command.
Like for example, a little while ago, I create a utility called `list-repos` which allowed me to check the status of the Git repositories in a directory. You can read more about it here:
`list-repos` does some cool stuff if you are working on so many open source projects with Git
I can ramble more about the utility I created, but that’s not important for this post here.
Important thing is that, to use this utility; you need to install it globally on your computer as the following command:
npm i -g list-repos
And then to use it, you need to execute the following command:
list-repos .. # from any project
list-repos # parent where all projects reside
Now with new versions of the npm, it installs another utility called `npx`
This utility will allow you to execute any executable package without installing it globally.
This means that now you don’t need to fire
npm i -g list-repos
So, how to use?npx
You need to provide the following things to
npx
:my-package
my-package
This means that, for
list-repos
, all you need to do is to fire following command:npx list-repos ..
You can pass the params to the binaries in a similar way you would pass the arguments to any bash utility.
Originally, npx started in May 2017; it was a npm package installable as other npm binaries from npx - npm
And now it is part of npm and installed by default.
So if your system says that
npx
is not found, you can npm i -g npm
npm
on current npm as npm i -g npx
If you are using nodejs with nvm, then it can be a bit tricky.
If you are using the npm version which internally supports npx
1. moving to a version which doesn’t, then
2. moving to a version which does have npx
If you are using npm version which internally doesn’t support npm
1. moving to a version which supports
2. moving to a version which also doesn’t support npx
-—reinstall-packages-from=<from-node-version>
; With new command as:
nvm install v6.9.2 --reinstall-packages-from=v4.4.5
Use aliases on your preferred terminal to assign some aliases to your favorite commands
alias lrs="npx list-repos"
If you have already installed any npm package globally on your computer, npx will pick it up from your global installation.
And if any package is added as a dependency in your node project and you are using npx in your npm scripts, npx will use the package form local dependency space i.e. node_modules
This give a chance to use packages like
yarn
, create-react-app
or any similar binary always from the latest version.(Almost) No need to reinstall the latest version and then retry to use the binaries.
npx is a cool utility to make use of in the daily development workflow. Though it still does not replace the globally installable package because is package is not installed, npx will always take the package from the internet.
And which might not be a very happy case of
And also it takes some time to download the package and its dependencies to execute locally.
So let me know how would you make use of npx and what do you think about this article through comments 💬 or on Twitter at @patel_pankaj_ and @time2hack
If you find this article helpful, please share it with others 🗣; subscribe to the blog for new posts and see you the next time.