Evaluate Clojure code via nREPL using clj-nrepl-eval. Use this when you need to test code, check if edited files compile, verify function behavior, or interact with a running REPL session.
Use this skill when you need to:
The clj-nrepl-eval command evaluates Clojure code against an nREPL server. Session state persists between evaluations, so you can require a namespace in one evaluation and use it in subsequent calls. Each host:port combination maintains its own session file.
First, discover what nREPL servers are running in the current directory:
clj-nrepl-eval --discover-ports
This will show all nREPL servers (Clojure, Babashka, shadow-cljs, etc.) running in the current project directory.
Then use the AskUserQuestion tool:
If ports are discovered: Prompt user to select which nREPL port to use:
If no ports are discovered: Prompt user how to start an nREPL server:
IMPORTANT: IF you start a REPL do not supply a port let the nREPL start and return the port that it was started on.
Evaluation automatically connects to the given port
Use the -p flag to specify the port and pass your Clojure code.
Recommended: Use heredoc via stdin to avoid shell escaping issues. The single-quoted delimiter (<<'EOF') passes all characters through literally.
clj-nrepl-eval -p <PORT> <<'EOF'
(+ 1 2 3)
EOF
For multiple expressions:
clj-nrepl-eval -p <PORT> <<'EOF'
(def x 10)
(+ x 20)
EOF
Discover all nREPL servers in current directory:
clj-nrepl-eval --discover-ports
Shows all running nREPL servers in the current project directory, including their type (clj/bb/basilisp) and whether they match the current working directory.
Check previously connected sessions:
clj-nrepl-eval --connected-ports
Shows only connections you have made before (appears after first evaluation on a port).
Require a namespace (always use :reload to pick up changes):
clj-nrepl-eval -p <PORT> "(require '[my.namespace :as ns] :reload)"
Test a function after requiring:
clj-nrepl-eval -p <PORT> "(ns/my-function arg1 arg2)"
Check if a file compiles:
clj-nrepl-eval -p <PORT> "(require 'my.namespace :reload)"
Multiple expressions:
clj-nrepl-eval -p <PORT> "(def x 10) (* x 2) (+ x 5)"
Complex multiline code:
clj-nrepl-eval -p <PORT> <<'EOF'
(def x 10)
(* x 2)
(+ x 5)
EOF
With custom timeout (in milliseconds):
clj-nrepl-eval -p <PORT> --timeout 5000 "(long-running-fn)"
Reset the session (clears all state):
clj-nrepl-eval -p <PORT> --reset-session
clj-nrepl-eval -p <PORT> --reset-session "(def x 1)"
-p, --port PORT - nREPL port (required)-H, --host HOST - nREPL host (default: 127.0.0.1)-t, --timeout MILLISECONDS - Timeout (default: 120000 = 2 minutes)-r, --reset-session - Reset the persistent nREPL session-c, --connected-ports - List previously connected nREPL sessions-d, --discover-ports - Discover nREPL servers in current directory-h, --help - Show help messageclj-nrepl-eval -p <PORT> <<'EOF' ... EOF to avoid shell escaping issues--reset-session only resets the nREPL session (clearing dynamic vars like *e, *1), not def'd vars or loaded namespaces:reload to pick up recent changesclj-nrepl-eval --discover-portsclj-nrepl-eval -p <PORT> "(require '[my.ns :as ns] :reload)"
clj-nrepl-eval -p <PORT> "(ns/my-fn ...)"
:reload, test again