You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在Emacs CIDER中运行Clojure程序报错:No such namespace: hello

问题

我尝试在Emacs中运行一个Clojure版的"hello world"程序,但遇到错误:No such namespace: hello。

在终端中启动程序完全正常:

u@h:~/hello-world$ clj -X hello/run
Hello world, the time is 07:52 am
u@h:~/hello-world$ 

但在Emacs配合CIDER时无法正常运行,这个程序取自Clojure官方指南。

项目目录结构如下:

u@h:~/hello-world$ find |grep -v cache 
.
./.clj-kondo
./src
./src/hello.clj
./.lsp
./.nrepl-port
./deps.edn

相关文件内容:

  • ./src/hello.clj
(ns hello
  (:require [java-time.api :as t]))

(defn time-str
  "Returns a string representation of a datetime in the local time zone."
  [instant]
  (t/format
    (t/with-zone (t/formatter "hh:mm a") (t/zone-id))
    instant))

(defn run [opts]
  (println "Hello world, the time is" (time-str (t/instant))))
  • ./deps.edn
{:deps
 {clojure.java-time/clojure.java-time {:mvn/version "1.4.2"}}}

我在Emacs中的操作步骤:

  • 按下 Ctrl+x Ctrl+f 打开~/hello-world/src/hello.clj
  • 按下 Ctrl+c Ctrl+x j j 连接REPL,出现连接信息后,输入(hello/run),得到错误:
user> (hello/run)
Syntax error compiling at (*cider-repl ~/hello-world:localhost:34699(clj)*:43:7).
No such namespace: hello
user> 

请问我在Emacs中的操作哪里出错了?


解决方法

问题出在REPL未加载hello命名空间:终端用clj -X运行时会自动定位并加载目标命名空间,但CIDER启动的REPL默认只会进入user命名空间,不会自动加载项目里的其他命名空间。

按以下步骤修正:

  1. 在打开的hello.clj文件中,按下 Ctrl+c Ctrl+k(对应CIDER的cider-load-buffer命令),把当前文件的hello命名空间加载到REPL环境中。
  2. 之后在REPL中调用函数时,需要给run传一个参数(因为你的run定义需要接收opts参数),输入:(hello/run {}) 即可正常执行。

或者更简便的方式:
加载命名空间后,按下 Ctrl+c Ctrl+n(对应cider-repl-set-ns命令),REPL会自动切换到hello命名空间,这时直接输入(run {})就能运行程序。

内容的提问来源于stack exchange,提问作者trzczy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.18 20:53:26