When first viewed
npx
, I think it's a typo on npm
.
Ternya not ...
NPM and NPX are two tools that have different functions.
What are the differences?
... and when do we have to use NPX?
Let's discuss it?
NPM vs. NPX
NPM stands for Node Package Manager . A text based program for Nodejs package management.
Whereas NPX is the Package Runner Code . Its function is to execute the Nodejs package.
NPX will execute binary files from the Nodejs package that has been installed or not.
Even NPX can also help us use certain versions of Nodejs without having to use nvm (nodejs version management) , nave (nodejs critical environment)and n (nodejs version management) . 1
NPX installation
NPX was added to the NPM version
5.2.0
. So, if you use this version of NPM, you don't need to install it npx
.
But if your computer isn't installed, you can install it with the command:
[sudo] npm install -g npx
Use it
sudo
if you install Nodejs in the root directory.How to use NPX
How to use NPX is almost the same as NPM.
For a list of other options and arguments, you can see with help
npx
.
Please type the command:
npx --help
To see help.
Let's try using
npx
...npx happy-birthday -u "Petani Kode"
Note,
happy-birthday
is the package name that we will execute. Then -u "Petani Kode"
is the argument for the package happy-birthday
.
Package
happy-birthday
is a package that serves to display text "Selamat ulang tahun"
in various languages.
The result:
When Should We Use NPX?
We can use NPX for some cases like this:
1. When I Want to Execute Once
We might only need a package node to be executed just once.
Example:
Package
create-react-app
to create the React application project.
Then we can use the command:
npx create-react-app nama-project
2. When I Want to Execute Script from Gist
We can use NPX to execute scripts from Gist.
Gist is a Github service for storing scripts like Pastebin.
Let's try to execute the script from Gist with NPX.
Try typing the following command:
npx https://gist.github.com/ardianta/df9a4c8be44b5d5f4bb1b67cdd13f4ea
Then the result:
3. When you want to use a different package version
Kada we work on a project that uses a different version of Pakacakge than the one installed on our computer.
Here we can use NPX to use a package version that matches the project.
To determine the package version, we just add it
@1.2.3
behind the package name. 1.2.3
is the version of the package that will be used.
Example:
Currently the
gulp
version has reached 4.0.1
, but I want to use the version of Gulp 3.9.0
.
So I have to type:
npx gulp@3.9.0 --version
The argument
--version
is an argument to check the version gulp
.
The result:
4. When you don't have root access
Sometimes we will find, times when we are not given permission to install the Nodejs package globally.
For example, on a server. We are only given permission to use it as a normal user.
Then to install the Nodejs package globally there, we need root access.
But unfortunately we don't have that access.
Now here is where we can use
npx
.Shell auto-fallback with NPX
Ever found something like this?
We want to execute the command
webpack
, but the command has not been installed on the computer.
We are advised to type the command:
sudo apt install webpack
This is shell-auto fallback.
The auto-fallback shell is a command that will be performed when a command is not found.
NPX has a fallback auto-auto feature. How to activate it:
For bash shell:
source <(npx --shell-auto-fallback bash)
For zsh shell:
source <(npx --shell-auto-fallback zsh)
For shell fish:
source (npx --shell-auto-fallback fish | psub)
Let's try ...
First we register NPX as an auto fallback on bash (because I use bash) with the command:
source <(npx --shell-auto-fallback bash)
Then I tried executing the command
gulp@3.9.0
and happy-birthday
.
Oh yeah, when I
happy-birthday
just typed ...
... the auto-fallback shell doesn't work.
Maybe this is not to clash with other Linux commands.
Actually, the command:
npx --shell-auto-fallback bash
Will generate a bash code for auto-fallback.
We can save this file
.bashrc
to Permenen.Running Server with NPX
We can also use NPX to run the server. You can try to package nodejs to create such servers
nodemon
, json-server
and others.Reference: https://www.petanikode.com/npm-vs-npx/
0 Komentar untuk "Learning Nodejs: What is NPX? and What's the Difference with NPM?"
Silahkan berkomentar sesuai artikel