Search code examples
emacsclojurecider

Starting a simple program in Clojure in emacs cider and getting "No such namespace: hello"


I try to run a clojure 'hello world'-like program with emacs but it gives the error No such namespace: hello.

When I start it in terminal it works good:

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

but in emacs with cider not.

The program is taken from the site: https://clojure.org/guides/deps_and_cli#_writing_a_program

I show the tree of the project:

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

and the content:

./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"}}}

Now how I deal with emacs:

CTRL + x CTRL + f ~/hello-world/src/hello.clj

CTRL + c CTRL + x j j

This appears:

;; Connected to nREPL server - nrepl://localhost:34699
;; CIDER 1.15.1 (Cogne), nREPL 1.2.0
;; Clojure 1.11.4, Java 17.0.12
;;     Docs: (doc function-name)
;;           (find-doc part-of-name)
;;   Source: (source function-name)
;;  Javadoc: (javadoc java-object-or-class)
;;     Exit: <C-c C-q>
;;  Results: Stored in vars *1, *2, *3, an exception in *e;
;; ======================================================================
;; If you’re new to CIDER it is highly recommended to go through its
;; user manual first. Type <M-x cider-view-manual> to view it.
;; In case you’re seeing any warnings you should consult the manual’s
;; "Troubleshooting" section.
;;
;; Here are a few tips to get you started:
;;
;; * Press <C-h m> to see a list of the keybindings available (this
;;   will work in every Emacs buffer)
;; * Press <,> to quickly invoke some REPL command
;; * Press <C-c C-z> to switch between the REPL and a Clojure file
;; * Press <M-.> to jump to the source of something (e.g. a var, a
;;   Java method)
;; * Press <C-c C-d C-d> to view the documentation for something (e.g.
;;   a var, a Java method)
;; * Print CIDER’s refcard and keep it close to your keyboard.
;;
;; CIDER is super customizable - try <M-x customize-group cider> to
;; get a feel for this. If you’re thirsty for knowledge you should try
;; <M-x cider-drink-a-sip>.
;;
;; If you think you’ve encountered a bug (or have some suggestions for
;; improvements) use <M-x cider-report-bug> to report it.
;;
;; Above all else - don’t panic! In case of an emergency - procure
;; some (hard) cider and enjoy it responsibly!
;;
;; You can remove this message with the <M-x cider-repl-clear-help-banner> command.
;; You can disable it from appearing on start by setting
;; ‘cider-repl-display-help-banner’ to nil.
;; ======================================================================
;;  Startup: /usr/local/bin/clojure -A:dev -Sdeps \{\:deps\ \{nrepl/nrepl\ \{\:mvn/version\ \"1.2.0\"\}\ cider/cider-nrepl\ \{\:mvn/version\ \"0.49.1\"\}\}\ \:aliases\ \{\:cider/nrepl\ \{\:main-opts\ \[\"-m\"\ \"nrepl.cmdline\"\ \"--middleware\"\ \"\[cider.nrepl/cider-middleware\]\"\]\}\}\} -M:cider/nrepl
user>

Then I type: (hello/run) and get:

user> (hello/run)
Syntax error compiling at (*cider-repl ~/hello-world:localhost:34699(clj)*:43:7).
No such namespace: hello
user> 

What do I do wrong in emacs?


Solution

  • You have to require the namespace first in order to be able to use it: (require 'hello).

    Note that single-segment namespaces are deprecated and you're likely to get complaints from your IDE/REPL/linter/whatever. You can fix it by creating a directory src/hello and moving src/hello.clj into e.g. src/hello/main.clj or src/hello/core.clj. Of course, you'll have to use hello.main or hello.core instead of just hello, but you can alias namespaces: (require '[hello.main :as hello]).