Search code examples
printingclojurescriptread-eval-print-loopshadow-cljs

Where is the "Hello world" going to be printed after following shadow-cljs documentation to watch the app?


I am following shadow-cljs Quick Start documentation on a minimal example of a project. Here is the link.

I have this shadow-cljs.edn file:

;; shadow-cljs configuration
{:source-paths
 ["src/dev"
  "src/main"
  "src/test"]

 :dev-http {8080 "public"}
 :dependencies
 []

 :builds
 {:frontend
  {:target :browser
   :modules {:main {:init-fn acme.frontend.app/init}}
   }}}

In /Users/pedro/projects/acme-app/src/main/acme/frontend/app.cljs, I also have:

(ns acme.frontend.app)

(defn init []
  (println "Hello World"))

I can build and watch it with the command:

$ npx shadow-cljs watch frontend


shadow-cljs - config: /Users/pedro/projects/acme-app/shadow-cljs.edn
shadow-cljs - HTTP server available at http://localhost:8080
shadow-cljs - server version: 2.20.2 running at http://localhost:9630
shadow-cljs - nREPL server started on port 61214
shadow-cljs - watching build :frontend
[:frontend] Configuring build.
[:frontend] Compiling ...
[:frontend] Build completed. (127 files, 0 compiled, 0 warnings, 6.97s)

Since the init function is a "Hellow World" function I was expecting to see it in some place. But, I can't find anywhere "showing" the Hello World to see it worked out.

Where is the "Hello World" supposed to "appear"? Is it supposed only to be available in the REPL inside the IDE as a function to be invoked by the programmer?

The "hello world" is not printed on the terminal (see above the message retrieved), is not displayed on the UI on localhost:8080 (that would probably need a tweak in HTML - see pic below), and does not appear on the browser console (that would probably need js/console.log).

These are the failed attempts to invoke the functions in the REPL after executing npx shadow-cljs node-repl:

cljs.user=> (acme.frontend.app/init)
------ WARNING - :undeclared-var -----------------------------------------------
 Resource: <eval>:1:2
 Use of undeclared Var acme.frontend.app/init
--------------------------------------------------------------------------------

cljs.user=> (main.acme.frontend.app/init)
------ WARNING - :undeclared-ns ------------------------------------------------
 Resource: <eval>:1:2
 No such namespace: main.acme.frontend.app, could not locate main/acme/frontend/app.cljs, main/acme/frontend/app.cljc, or JavaScript source providing "main.acme.frontend.app"
--------------------------------------------------------------------------------

This is the image on localhost:8080:

enter image description here


Solution

  • You appear to have skipped the creation of the public/index.html, as mentioned in the Quick Start. The build is using :target :browser, so it is meant to be loaded in the Browser. The HTML will do that. When loaded you'll see the println in the Browser Console.

    For the REPL the code always needs to be loaded before if can be used. So, you need a (require '[acme.frontend.app :as x]) before running (x/init).