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
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