简体   繁体   中英

Can't Use NodeJS Twitter Library in ClojureScript 1.8 Lein Project

I'm developing a ClojureScript project from the starter template that I scaffolded with lein new cljs-lambda my-proj .

The project.clj looks like this:

(defproject my-proj "0.1.0-SNAPSHOT"
  :description "Jim's bot"
  :url "http://jim.derp"
  :dependencies [[org.clojure/clojure       "1.8.0"]
                 [org.clojure/clojurescript "1.8.51"]
                 [org.clojure/core.async    "0.2.395"]
                 [io.nervous/cljs-lambda    "0.3.5"]
                 [cljs-http "0.1.44"]]
  :plugins [[lein-cljsbuild "1.1.4"]
            [lein-npm       "0.6.0"]
            [lein-doo       "0.1.7"]
            [io.nervous/lein-cljs-lambda "0.6.6"]]
  :npm {:dependencies [[source-map-support "0.4.0"]
                       [xhr2 "0.1.4"]
                       [twitter "1.7.1"]
                       ]}

  :source-paths ["src"]
  :cljs-lambda
  {:defaults      {:role "arn:aws:iam::954459734159:role/cljs-lambda-default"}
   :resource-dirs ["static"]
   :functions
   [{:name   "cljs-twitter-unfollower"
     :invoke twitter-unfollower.core/run-lambda}]}
  :cljsbuild
  {:builds [{:id "twitter-unfollower"
             :source-paths ["src"]
             :compiler {:output-to     "target/twitter-unfollower/twitter_unfollower.js"
                        :output-dir    "target/twitter-unfollower"
                        :source-map    true
                        :target        :nodejs
                        :language-in   :ecmascript5
                        :optimizations :none}}
            {:id "twitter-unfollower-test"
             :source-paths ["src" "test"]
             :compiler {:output-to     "target/twitter-unfollower-test/twitter_unfollower.js"
                        :output-dir    "target/twitter-unfollower-test"
                        :target        :nodejs
                        :language-in   :ecmascript5
                        :optimizations :none
                        :main          twitter-unfollower.test-runner
                        }
             }]})

```

Under :npm {:dependencies } I have added [twitter "1.7.1"] to bring in my external javascript library, twitter whose install command is npm i twitter --save and latest version is "1.7.1". When I run lein deps I can indeed see my libraries being download into a node_modules folder. Yay! My issue is now using this from within my clojurescript files...

To this question short and simple let's suppose my project only has a single clojurescript file, core.cljs, and inside of it is this code:

(ns twitter-unfollower.core
  (:require [cljs-lambda.util :as lambda]
            [cljs-lambda.context :as ctx]
            [cljs-lambda.macros :refer-macros [deflambda]]
            [cljs-http.client :as http]
            [cljs.nodejs :as nodejs]
            [cljs.core.async :as async :refer [<!]])

(defn doStuff []
  (println "searching for tweets...")

  ;; fetch tweets here!

)

(deflambda run-lambda []
  (doStuff))

When I try to then add twitter by putting `[twitter :as twitter] with the other things inside of the :require form it throws the error:

Compiling "target/twitter-unfollower/twitter_unfollower.js" from ["src"]...
Compiling "target/twitter-unfollower/twitter_unfollower.js" failed.
clojure.lang.ExceptionInfo: failed compiling file:src/twitter_unfollower/core.cljs {:file #object[java.io.File 0x1b641c97 "src/twitter_unfollower/core.cljs"]}
...
...
...
Caused by: clojure.lang.ExceptionInfo: No such namespace: twitter, could not locate twitter.cljs, twitter.cljc, or Closure namespace "twitter" in file src/twitter_unfollower/core.cljs {:tag :cljs/analysis-error}

Is this not the correct way to add nodejs modules in the cljs code? Do I need to perhaps use a nodejs version of require as opposed to the clojurescript require? Thanks!

I can't repro. I added a closing paren on the ns form for twitter-unfollower.core .

I first ran:

lein npm install

Then:

$ lein cljsbuild once Compiling ClojureScript... Compiling "target/twitter-unfollower/twitter_unfollower.js" from ["src"]... Successfully compiled "target/twitter-unfollower/twitter_unfollower.js" in 5.871 seconds.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM