Search code examples
clojureclojurescript

"Address already in use" when compiling ClojureScript


I am following the ClojureScript QuickStart Guide and when executing the "build and run" step

clj --main cljs.main --compile hello-world.core --repl

get the error:

Execution error (BindException) at java.net.PlainSocketImpl/socketBind (PlainSocketImpl.java:-2).
Address already in use (Bind failed)

Full report at:
/var/folders/c0/ykv1h_th8xl1tt006s7jd2lh0000gq/T/clojure-2487776791711977744.edn

The full report looks like this:

{:clojure.main/message
 "Execution error (BindException) at java.net.PlainSocketImpl/socketBind (PlainSocketImpl.java:-2).\nAddress already in use (Bind failed)\n",
 :clojure.main/triage
 {:clojure.error/class java.net.BindException,
  :clojure.error/line -2,
  :clojure.error/cause "Address already in use (Bind failed)",
  :clojure.error/symbol java.net.PlainSocketImpl/socketBind,
  :clojure.error/source "PlainSocketImpl.java",
  :clojure.error/phase :execution},
 :clojure.main/trace
 {:via
  [{:type java.net.BindException,
    :message "Address already in use (Bind failed)",
    :at
    [java.net.PlainSocketImpl socketBind "PlainSocketImpl.java" -2]}],
  :trace
  [[java.net.PlainSocketImpl socketBind "PlainSocketImpl.java" -2]
   [java.net.AbstractPlainSocketImpl
    bind
    "AbstractPlainSocketImpl.java"
    387]
   [java.net.ServerSocket bind "ServerSocket.java" 375]
   [java.net.ServerSocket <init> "ServerSocket.java" 237]
   [java.net.ServerSocket <init> "ServerSocket.java" 128]
   [cljs.repl.server$start invokeStatic "server.clj" 215]
   [cljs.repl.server$start invoke "server.clj" 212]
   [cljs.repl.browser$setup$fn__7717 invoke "browser.clj" 381]
   [cljs.repl.browser$setup invokeStatic "browser.clj" 357]
   [cljs.repl.browser$setup invoke "browser.clj" 356]
   [cljs.repl.browser.BrowserEnv _setup "browser.clj" 392]
   [cljs.repl$repl_STAR_$fn__6906 invoke "repl.cljc" 1121]
   [cljs.compiler$with_core_cljs invokeStatic "compiler.cljc" 1417]
   [cljs.compiler$with_core_cljs invoke "compiler.cljc" 1406]
   [cljs.repl$repl_STAR_ invokeStatic "repl.cljc" 1119]
   [cljs.repl$repl_STAR_ invoke "repl.cljc" 1031]
   [cljs.cli$repl_opt invokeStatic "cli.clj" 316]
   [cljs.cli$repl_opt invoke "cli.clj" 303]
   [cljs.cli$default_compile invokeStatic "cli.clj" 504]
   [cljs.cli$default_compile invoke "cli.clj" 461]
   [cljs.cli$compile_opt invokeStatic "cli.clj" 510]
   [cljs.cli$compile_opt invoke "cli.clj" 508]
   [cljs.cli$main invokeStatic "cli.clj" 651]
   [cljs.cli$main doInvoke "cli.clj" 640]
   [clojure.lang.RestFn applyTo "RestFn.java" 139]
   [clojure.core$apply invokeStatic "core.clj" 667]
   [clojure.core$apply invoke "core.clj" 660]
   [cljs.main$_main invokeStatic "main.clj" 61]
   [cljs.main$_main doInvoke "main.clj" 52]
   [clojure.lang.RestFn applyTo "RestFn.java" 137]
   [clojure.lang.Var applyTo "Var.java" 705]
   [clojure.core$apply invokeStatic "core.clj" 665]
   [clojure.main$main_opt invokeStatic "main.clj" 514]
   [clojure.main$main_opt invoke "main.clj" 510]
   [clojure.main$main invokeStatic "main.clj" 664]
   [clojure.main$main doInvoke "main.clj" 616]
   [clojure.lang.RestFn applyTo "RestFn.java" 137]
   [clojure.lang.Var applyTo "Var.java" 705]
   [clojure.main main "main.java" 40]],
  :cause "Address already in use (Bind failed)"}}

I do not see the actual address conflict. How can I find out what went wrong?


Solution

  • When you use cljs.main --repl, that starts a browser-based REPL that communicates with the service via TCP -- thus, requiring a port to be opened.

    You can change the port number:

    cljs.main --port 13124 --compile hello-world.core --repl
    

    ...will use 13124 instead of the default 9000. (You can also use your local operating system's equivalent to netstat --listen -nep | grep 9000 to see which process has the default port open, and kill that process if you so choose).