Gleam Typed BEAM and JS language

Version 1.19.2 · source

Start here

gleam new <dir>
Create a project
gleam run
Compile and run main
gleam test
Run the test suite
gleam add <pkg>
Add a dependency
gleam check
Type-check only
gleam format
Format the code
gleam build --target javascript
Compile to JS
gleam help <cmd>
Show command help

Concepts

  1. src/*.gleam your modules
  2. build/ BEAM files or JS
  3. main() entry point
From source to running code

Projects

gleam new <dir>
Create a new package
--template javascript sets the JS target.
gleam new <dir> --skip-git
Skip .git and .github
--skip-github omits only .github.
gleam run
Compile and run main
gleam run --target javascript
Run on the JS target
gleam run --runtime <rt>
JS runtime: nodejs, deno or bun
gleam shell
Erlang REPL with your code loaded
It speaks Erlang syntax, not Gleam.
gleam clean
Delete the build directory
gleam dev
Run main of <package>_dev

Build & check

gleam build
Compile package and dependencies
gleam build --target <t>
Pick erlang or javascript
gleam build --warnings-as-errors
Fail on any warning
Handy in CI.
gleam check
Type-check without code generation
gleam export erlang-shipment
Directory with bytecode and start script

Test & format

gleam test
Run main of <package>_test
gleam test --target <t>
Test on one target
gleam format
Format the project
gleam format <path>...
Format given files or dirs
gleam format --check
Fail if a file is unformatted
Use it in CI.

Dependencies

gleam add <pkg>
Add latest compatible version
gleam add <pkg>@<version>
Pin a major or full version
Example: gleam add gleam_json@3
gleam add --dev <pkg>
Add as a dev dependency
gleam remove <pkg>
Remove a dependency
gleam deps download
Fetch deps, write manifest.toml
Normally runs automatically.
gleam deps list
List all dependency versions
gleam deps outdated
Show dependencies behind latest
gleam deps update [<pkg>...]
Upgrade within gleam.toml limits
Does not edit gleam.toml.
gleam update
Alias for deps update
gleam deps tree
Print the dependency tree
gleam deps tree --invert <pkg>
Who depends on this package

Syntax basics

let x = 1
Immutable binding
const limit = 10
Top-level constant
Value must be a literal.
x |> f |> g
Pipe into the first argument
case v { 0 -> a _ -> b }
Exhaustive pattern match
[first, ..rest]
List pattern or prepend
"pre" <> rest
String prefix pattern, concat
pub type T { A(n: Int) B }
Custom type with variants
Person(..p, age: 30)
Record update
fn f(x: Int, add n: Int) -> Int
Labelled argument
f(1, add:)
Label shorthand when names match
use x <- result.try(f())
Flatten callbacks
Rest of the block becomes the callback.
Ok(v) / Error(e)
Result variants
Some(v) / None
Option, from gleam/option
import gleam/io.{println}
Import module and items
<<1, 2>>
Bit array literal
let assert Ok(v) = r
Crash if the pattern fails
todo as "msg"
Placeholder that crashes if run
assert a == b
Boolean check, used in tests
echo value
Print a debug value
@external(erlang, "m", "f")
Bind to a native function

Publishing & docs

gleam docs build --open
Build HTML docs and open them
gleam publish
Publish the package to Hex
gleam publish --replace
Overwrite a release under 1 hour old
gleam hex authenticate
Log in to Hex
gleam docs publish
Publish docs to HexDocs
gleam hex retire --package <p> --version <v> --reason <r> <msg>
Retire a release
Reasons: other, invalid, security, deprecated, renamed.
gleam hex revert --package <p> --version <v>
Remove a release within 24 hours

Recipes

Start a project and add libraries
gleam new app && cd appgleam add gleam_stdlib gleam_jsongleam add --dev gleeunitgleam test
CI check
gleam format --checkgleam build --warnings-as-errorsgleam test
Same tests on both targets
gleam test --target erlanggleam test --target javascript
Pipeline with use and Result
pub fn load(id) {  use user <- result.try(find(id))  use age <- result.try(parse(user.age))  Ok(age + 1)}

Help

gleam help
List all commands
gleam help <command>
Help for one command
gleam --version
Print the compiler version
gleam lsp
Start the language server
Editors launch it for you.