Execute gist with npx


This post goes over how to execute a GitHub Gist with npx.

package.json

A valid package.json needs at minimum:

  • name
  • version

For example:

{
  "name": "example",
  "version": "1.0.0"
}

npx

The minimum of an npx script requires all of the above plus:

  • bin

So if you have index.js:

#!/usr/bin/env node

console.log('Hello index.js');

The shebang line is required on Unix-like platforms. Otherwise, npx will complain with:

npx: command not found: ...

Then you can update your package.json with:

{
  "name": "example",
  "version": "1.0.0",
  "bin": "./index.js"
}

Gist

If you have the above saved in a GitHub repository or Gist, you can execute it with npx:

npx <link>

This is similar to how you can install npm packages from GitHub or Gist.

Example

npx https://gist.github.com/remarkablemark/91ff0418d178c9e76d80203a6b9c6f1a
npx: installed 1 in 3.107s
Hello index.js

Gist:



Please support this site and join our Discord!