Search code examples
clojurepackageleiningen

Finding out the name to `:require` in a namespace


I am following this tutorial: https://practicalli.github.io/blog/posts/web-scraping-with-clojure-hacking-hacker-news/ and I have had a hard time dealing with the :require part of the ns macro. This tutorial shows how to parse HTML and pull out information from it with a library called enlive, and to use it, I first had to put

   ...
  :dependencies [[org.clojure/clojure "1.10.1"]
                 [enlive "1.1.6"]]
   ...

in my project.clj, and require the library in core.clj as the following:

(ns myproject.core
  (:require [net.cgrand.enlive-html :as html])
  (:gen-class))

I spent so much time finding out the name net.cgrand.enlive-html, since it was different from the package's name itself (which is just enlive), and I couldn't find it through any of the lein commands (I eventually found it out by googling).

How can I easily find out what name to require?


Solution

  • Practical approach

    If your editor/IDE helps with auto-completion and docs, that might be a first route.

    Other than that, libraries usually have some read-me online, where they show off what they do (what to require, how to use that).

    Strict approach

    If you really have nothing about a library, you will find the downloaded library in you ~/.m2/repository directory. Note that deps without the naming convention of "group/artifact" will just double on the artifact name, Next is the version. So you can find your libraries JAR file here: .m2/repository/enlive/enlive/1.1.6/enlive-1.1.6.jar.

    JAR files are just ZIP-Files. Inside the JAR file you will usually find the source files of the library. The directory structure reflects the package structure. E.g. one file there is net/cgrand/enlive_html.clj (note the use of the _ instead of -, this is due to name munging for the JVM). You then can require the file in your REPL and explore with doc or dir etc. Or you open this file, to see the docs and source code in one chunk.