šŸ‘©ā€šŸ’» chrismanbrown.gitlab.io

Re: Making a single-file executable with node and esbuild

or Make a singe-file executable with deno

2024-01-29

INTRODUCTION

This is a response to ā€œMaking a single-file executable with node and esbuildā€ by Bill Mill of billmill.org, which is a fantastic write-up of the somewhat complicated steps required to make a single-file executable with node.js and esbuild.

I want to begin by hedging my response and acknowledging the disingenuousness of offering ā€œjust use denoā€ when A) using deno, and potentially B) ā€œmost simple and least number of stepsā€ are probably not the point of Bill’s post. The point is most likely to document the steps for making a single-file executable with node.js and esbuild. Which he more than accomplishes.

This is offered in case somebody else out there also wants to make a javascript executable, and wants to consider an alternative to the process described in Bill’s post.

MAKE A SINGE-FILE EXECUTABLE WITH DENO

Let us also begin with a simple two file program that uses a dependency.

sum.js

export function sum(ns) { return ns.reduce((x,y) => x+y, 0) }

index.js

import { parseArgs } from "https://deno.land/std@0.213.0/cli/parse_args.ts";
import { sum } from "./sum.js";

console.dir(sum(parseArgs(Deno.args)._));

And it works thusly:

āÆ deno run index.js 1 2 3 4 5 6
21

Slightly off topic, but notice there is no npm init or npm install steps. Deno is designed to not use npm, or node_modules.

Now, let us commence with the compiling part:

āÆ deno compile --output sum index.js
Compile file:///Users/cb/sandbox/denoexec/index.js to sum

And it works thusly:

āÆ ./sum 1 2 3 4 5 6
21

The end!

RESOURCES