DEV Community

umamaheswararao tumma
umamaheswararao tumma

Posted on

Executing Gists With Npx

We all know that Gists are a very good way to share code templates or embed code in our articles or website.

If you don't know about Gists see this

Apart from that, we can use Gists to create smaller and single-file scripts.

To add more power, with the help of Npx, consumers can run these scripts from their command line.

Let's consider a small example. Below are the Index.js and package.json files from my public gist.

The Link to the above gist is here

Make sure you have npm installed on your machine. If not installed refer to this

Now open your command line/terminal, copy the below command, and paste

 

npx https://gist.github.com/umamahesh51/797c37a1d51a1affdee01a96c74ebe33
Enter fullscreen mode Exit fullscreen mode

Hit enter, You will see the ok to proceed question Like below.
Npx Command usage
type y
You will see the output like this.

Output after using NPX to run gist

in the above example we have "bin":"./index.js" so the index.js file will be executed.

It is the same as using the command

node index.js
Enter fullscreen mode Exit fullscreen mode

But instead of downloading the repository and then run the command, we are executing the package directly.

It will be applicable to GitHub repositories as well.
Try the npx with this repo

Use below command

npx https://github.com/umamahesh51/TestGist 
Enter fullscreen mode Exit fullscreen mode

A famous and complex example of this case is create-react-app

How we can utilize this feature of gists?

We can provide scripts that will do some useful things and share them with others, instead of building them and giving executable files, we can just provide a link to gist.

Repositories will be useful if we have a complex task like create-react-app. Where as gists are a way to provide small scripts.

My favorite useful gist which we can run using npx is
Export @code Extensions to a Markdown List
Try it and see.

That's all I have. Let me know if you have seen some interesting usages of Gists.

Latest comments (1)

Collapse
 
umamahesh51 profile image
umamaheswararao tumma

See me writing code for 365 days
github.com/umamahesh51/365DaysOfCo...