- Have you ever experienced installing CLI tools
from npm packages that you use only once
in order to create new projects?
Well, a good examples are Yeoman
or the create-react-app tool or the Angular CLI Tool.
What can be solved from installing these tools
is package pollution in your global directories
and most of us really go checking on
what is installed globally, if ever.
The main purpose of creating NPX was to resolve these issues
And as you see there are many use cases npx can be used.
I will explore a few.
Also make sure you have npm version 5.2 or above
to have access to NPX.
So let's take a look at a few commands you can do with NPX
and how you would use it.
Let's start with Angular for example,
I don't have the Angular CLI tool in my system
but I'd like to get a project go on
without installing the tool, so I can do
npx...
- p...
to select the package that I want to
temporarily install while we're creating
a new Angular project and then use the package...
I want, and then the command of that package.
So, ng...
new...
myapp.
What this command would do is
install temporarily that tool here
so the angular CLI with the -p or package
and then use the command from that package
to create a new app.
And I don't have that package installed in my system yet.
If we scroll back up to everything that just happened,
it temporarily installed Angular
and then created a new app.
And I don't have that particular package
in my system in my global directory.
And then I have my new app which is an Angular app
created in my system here, let me delete that.
Let me show you another use case, if we clear that up.
Let's say I had some unit tests from mocha.
I could do
npx
mocha
without actually installing mocha in my application.
Now I'm gonna get some errors because
I don't have any tests but if I had a test file
then it would run the tests that mocha usually runs.
Let's say, and this is a popular example with npx,
let's say for example I wanna use the package
to simply do something cool like
npx...
cowsay...
hello.
Then it's going to temporarily install cowsay
and then run the package
with the command that comes with that package.
You could also create scripts that run full npx commands,
let's say for example we go in our scripts...
and do something like...
do the command that we just did,
let's say we go all the way back to our npx command...
to copy that...
create a new script called...
create...
angular app...
and then add the script, and let me just put a comma
at the end of the line 8.
Now I got a new script that I can run with
npm...
run...
create...
ang...
and this will basically run...
and let's make sure we save our file first
and then run the script again
and then it's going to do the same thing again,
it's going to create a brand new app.
If you really think about it,
there are multiple cases where you can use this.
If you wanna have more detail you can go to this page
npmjs.com/package/npx
and it's gonna give you all the commands
and all the actual details of what you can do
with this package.