简体   繁体   English

无法在ClojureScript 1.8 Lein项目中使用NodeJS Twitter库

[英]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 . 我正在使用lein new cljs-lambda my-proj的入门模板开发ClojureScript项目。

The project.clj looks like this: project.clj看起来像这样:

(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". :npm {:dependencies }我添加了[twitter "1.7.1"]以引入我的外部Javascript库, twitter,其安装命令为npm i twitter --save ,最新版本为“ 1.7.1”。 When I run lein deps I can indeed see my libraries being download into a node_modules folder. 当我运行lein deps时,的确可以看到我的库正在下载到node_modules文件夹中。 Yay! 好极了! My issue is now using this from within my clojurescript files... 我的问题是现在从我的clojurescript文件中使用它...

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: 为了简短而简单地回答这个问题,我们假设我的项目只有一个clojurescript文件core.cljs,并且在其中是以下代码:

(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: 当我尝试通过将`[twitter:as twitter]与其他其他东西添加到:require表单中来添加twitter时,将引发错误:

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? 这不是在cljs代码中添加nodejs模块的正确方法吗? Do I need to perhaps use a nodejs version of require as opposed to the clojurescript require? 我是否需要使用nodejs版本的require而不是clojurescript require? Thanks! 谢谢!

I can't repro. 我不能复制。 I added a closing paren on the ns form for twitter-unfollower.core . 我在ns表单上为twitter-unfollower.core 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.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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