0
0
DenoHow-ToBeginner ยท 3 min read

How to Use deno compile to Create Executables

Use deno compile followed by your script file to create a standalone executable. You can specify the output file name with --output and target platforms with --target.
๐Ÿ“

Syntax

The basic syntax of deno compile is:

  • deno compile [options] <file> - Compiles the Deno script into a standalone executable.
  • --output <file> - Sets the name of the output executable file.
  • --target <platform> - Specifies the target platform like x86_64-unknown-linux-gnu, x86_64-pc-windows-msvc, or x86_64-apple-darwin.
  • --allow-net, --allow-read, etc. - Permissions your script needs at runtime.
bash
deno compile [options] <file>
๐Ÿ’ป

Example

This example compiles a simple Deno script hello.ts into an executable named hello. The script prints a greeting message.

bash
echo "console.log('Hello from Deno!')" > hello.ts
deno compile --output hello hello.ts
./hello
Output
Hello from Deno!
โš ๏ธ

Common Pitfalls

Common mistakes when using deno compile include:

  • Not specifying required permissions like --allow-net or --allow-read, causing runtime errors.
  • Forgetting to set --output, which defaults to the script name but may cause confusion.
  • Trying to compile scripts with unsupported dynamic imports or unstable APIs.
  • Not matching the --target platform to your system, leading to incompatible executables.
bash
deno compile hello.ts
# Might fail if permissions are needed

deno compile --allow-net --output hello hello.ts
# Correct usage with permissions and output
๐Ÿ“Š

Quick Reference

OptionDescription
--output <file>Set the output executable file name
--target <platform>Specify target platform for cross-compilation
--allow-netAllow network access at runtime
--allow-readAllow file system read access
--unstableEnable unstable APIs if used in script
โœ…

Key Takeaways

Use deno compile to create standalone executables from Deno scripts.
Always specify necessary permissions with flags like --allow-net to avoid runtime errors.
Use --output to name your executable clearly.
Match --target to your platform for compatibility.
Avoid dynamic imports or unstable APIs unless using --unstable flag.