Blog for Learning

| lesson material | material summary | questions and answers | definitions | types and examples | other information | materi pelajaran | ringkasan materi | pertanyaan dan jawaban | definisi | jenis-jenis dan contoh-contoh | informasi lainnya |

Powered by Blogger.


Learning Nodejs: What is NPX? and What's the Difference with NPM?

Npm vs. Npx Node
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 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.
NPX demo for execution of the binary package nodejs
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
Using a version of nodejs that is different from NPX

NPX installation

NPX was added to the NPM version 5.2.0So, 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 sudoif you install Nodejs in the root directory.

How to use NPX

How to use NPX is almost the same as NPM.
How to use npx
For a list of other options and arguments, you can see with help npx.
Please type the command:
npx --help
To see help.
Help for NPX
Let's try using npx...
npx happy-birthday -u "Petani Kode"
Note, happy-birthdayis the package name that we will execute. Then -u "Petani Kode"is the argument for the package happy-birthday.
We can get this argument by reading the readme from this package on the npmjs website .
Package happy-birthdayis a package that serves to display text "Selamat ulang tahun"in various languages.
The result:
Use NPX for execution of pacakge

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.
Package create-react-appto 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.
I will use the script that has been prepared .
#! / usr / bin / env node
for ( var i = 0 ; i < 10 ; i ++ ) {
console . log ( " Hello World! " );
view rawhello-world.js hosted with ❤by GitHub
" name " : " hello-world " ,
" version " : " 0.0.0 " ,
" bin " : " ./hello-world.js "
view rawpackage.son hosted with ❤by GitHub
Try typing the following command:
Then the result:
The results of the gist script execution

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.3behind the package name. 1.2.3is the version of the package that will be used.
Currently the gulpversion 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 --versionis an argument to check the version gulp.
Oh yeah, this gulp is a build tool. You can read the Gulp tutorial here .
The result:
Using the package version specified with npx

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?
The webpack command was not found
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 ...
Shell auto fallback npx
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-birthdayjust 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 nodemonjson-serverand others.

0 Komentar untuk "Learning Nodejs: What is NPX? and What's the Difference with NPM?"

Silahkan berkomentar sesuai artikel

Template By Kunci Dunia
Back To Top