BunJS SEA
BunJS is a JavaScript runtime with support for compiling scripts into self-contained executables.
SheetJS is a JavaScript library for reading and writing data from spreadsheets.
This demo uses the Bun compiler and SheetJS to create a standalone CLI tool for parsing spreadsheets and generating CSV rows.
It is strongly recommended to install BunJS on systems using SheetJS libraries in command-line tools. This workaround should only be considered if a standalone binary is considered desirable.
Great open source software grows with user tests and reports. Any issues should be reported to the BunJS project for further diagnosis.
Integration Details
The SheetJS BunJS module can be imported from BunJS scripts.
bun build --compile
generates a standalone executable that includes the BunJS
runtime, user JS code and supporting scripts and assets
Script Requirements
Scripts that exclusively use SheetJS libraries and BunJS built-in modules can be bundled using BunJS. The module should be required directly:
const XLSX = require("xlsx");
For example, the following script accepts one command line argument, parses the
specified file using the SheetJS readFile
method1, generates CSV text from
the first worksheet using sheet_to_csv
2, and prints to terminal:
const XLSX = require("xlsx");
/* process.argv[2] is the first argument to the script */
const filename = process.argv[2];
/* read file */
const wb = XLSX.readFile(filename);
/* generate CSV of first sheet */
const ws = wb.Sheets[wb.SheetNames[0]];
const csv = XLSX.utils.sheet_to_csv(ws);
/* print to terminal */
console.log(csv);
Complete Example
This demo was last tested in the following deployments:
Architecture | BunJS | Date |
---|---|---|
darwin-x64 | 1.1.10 | 2024-05-28 |
darwin-arm | 1.1.10 | 2024-05-29 |
win10-x64 | 1.1.12 | 2024-06-10 |
win11-arm | 1.1.33 | 2024-10-25 |
linux-x64 | 1.1.12 | 2024-06-09 |
linux-arm | 1.1.12 | 2024-06-10 |
BunJS on Windows on ARM uses the X64 compatibility layer. It does not generate a native ARM64 binary!
-
Install or update BunJS.3
-
Download the test file https://docs.sheetjs.com/pres.numbers:
curl -o pres.numbers https://docs.sheetjs.com/pres.numbers
-
Save the contents of the
sheet2csv.ts
code block tosheet2csv.ts
in the project folder. -
Install the SheetJS dependency:
bun install https://cdn.sheetjs.com/xlsx-0.20.3/xlsx-0.20.3.tgz
In some test runs, the command failed with a ENOTEMPTY
error:
error: InstallFailed extracting tarball for https://cdn.sheetjs.com/xlsx-0.20.1/xlsx-0.20.1.tgz
error: moving "https://cdn.sheetjs.com/xlsx-0.20.1/xlsx-0.20.1.tgz" to cache dir failed
ENOTEMPTY: Directory not empty (NtSetInformationFile())
The workaround is to prepend xlsx@
to the URL:
bun install xlsx@https://cdn.sheetjs.com/xlsx-0.20.3/xlsx-0.20.3.tgz
- Test the script with
bun run
:
bun run sheet2csv.ts pres.numbers
The script should display CSV contents from the first sheet:
Name,Index
Bill Clinton,42
GeorgeW Bush,43
Barack Obama,44
Donald Trump,45
Joseph Biden,46
- Compile and run
sheet2csv
:
bun build ./sheet2csv.ts --compile --outfile sheet2csv
./sheet2csv pres.numbers
The program should display the same CSV contents as the script (from step 2)
Footnotes
-
See "Installation" in the BunJS documentation for instructions. ↩