Fork me on GitHub
#re-frame
<
2022-01-20
>
roelof12:01:06

is there is way I can install all depencies from package.json ?

vanelsas12:01:27

npm install

roelof12:01:28

tried but still this annoying error message

The required namespace "ajax.core" is not available, it was required by "toy_project1/events.cljs".
I also tried npm install ajax and npm install ajax.core but no luck

p-himik12:01:04

What does the problematic :require look like?

roelof12:01:14

like this :

[ajax.core :as ajax]))

p-himik12:01:35

If that's the code that you wrote yourself - why did you put that vector there in the first place, where did you get it?

roelof12:01:00

no, it from another project

p-himik12:01:13

Does that project depend on cljs-ajax?

roelof12:01:05

not that I know this is the package.json

{
  "name": "reagent-projects",
  "version": "0.0.1",
  "private": true,
  "devDependencies": {
    "shadow-cljs": "2.16.6"
  },
  "dependencies": {
    "ajax": "0.0.4",
    "highlight.js": "10.7.1",
    "react": "^17.0.2",
    "react-dom": "^17.0.2"
  }
}

p-himik12:01:26

Also, you say that it's another project but note that the error mentions toy_project1/events.cljs. You haven't given us much context, but just the error alone suggests that you want to use cljs-ajax but you forgot to put it in your project's dependency list.

p-himik12:01:46

The error has nothing to do with NPM or package.json. cljs-ajax is a CLJS dependency, not an NPM one.

p-himik12:01:55

Also, if you installed that ajax with NPM just to try to fix this error then remove it before you forget. :) And in general, try to avoid installing anything via NPM unless you're 100% you need it. Even if you install and immediately uninstall something, your whole dependency list might be different because of the way NPM dependency resolution works.

roelof13:01:32

oke, how do I then install that cljs-ajax thing

p-himik13:01:09

With CLJS, you don't install it, you specify it as a dependency. Same way you have already done it with reagent and re-frame.

roelof13:01:59

oke, then I have to look for a version number to use

roelof13:01:34

still the error

roelof13:01:52

I have added the package to the json file

{
  "name": "reagent-projects",
  "version": "0.0.1",
  "private": true,
  "devDependencies": {
    "shadow-cljs": "2.16.6"
  },
  "dependencies": {
    "ajax": "0.0.4",
    "highlight.js": "10.7.1",
    "react": "^17.0.2",
    "react-dom": "^17.0.2",
    "cljs-ajax": "0.7.5"
  }
}

roelof13:01:33

and I need the package in this file

(ns toy-project1.events
  (:require
   [re-frame.core :as re-frame]
   [toy-project1.db :as db]
   [toy-project1.config :as config]
   [ajax.core :as ajax]))

(re-frame/reg-event-db
 ::initialize-db
 (fn [_ _]
   db/default-db))

(re-frame/reg-event-db
 ::http-fail
 (fn [db [_ key-path]]
   (assoc-in db key-path {})))

(re-frame/reg-event-db
 ::http-success
 (fn [db [_ key-path result]]
   (assoc-in db key-path result)))

(re-frame/reg-event-fx
 ::fetch-weather
 (fn [_ _]
   {:http-xhrio
    {:method :get
     :uri    ""
     :params {:lat   (:lat config/home)
              :lon   (:lon config/home)
              :units "imperial"
              :appid config/open-weather-api-key}
     :response-format (ajax/json-response-format {:keywords? true})
     :on-success      [::events/http-success [:weather]]
     :on-failure      [::events/http-fail [:weather]]}}))

roelof13:01:05

but still this outcome

npx shadow-cljs watch app
shadow-cljs - config: /home/roelof/reagent-projects/shadow-cljs.edn
shadow-cljs - HTTP server available at 
shadow-cljs - server version: 2.16.6 running at 
shadow-cljs - nREPL server started on port 41661
shadow-cljs - watching build :app
[:app] Configuring build.
[:app] Compiling ...
[:app] Build failure:
The required namespace "ajax.core" is not available, it was required by "toy_project1/events.cljs".

p-himik13:01:34

> I have added the package to the json file As I said here: > cljs-ajax is a CLJS dependency, not an NPM one. and here: > Same way you have already done it with reagent and re-frame. it shouldn't be added to package.json. It should be added to deps.edn or project.clj or shadow-cljs.edn - whatever you're using to specify your CLJS dependencies.

p-himik13:01:09

NPM dependencies and CLJS dependencies are two completely different worlds that CLJS build tools know how to use together. They don't interoperate in any ways, so NPM dependencies have to be specified via NPM ways, and CLJS dependencies have to be specified via CLJS ways.

roelof13:01:46

Thanks, it almost compiles

roelof13:01:29

another problem

roelof13:01:40

I have this in my events file

(re-frame/reg-event-db
 ::http-fail
 (fn [db [_ key-path]]
   (assoc-in db key-path {})))

(re-frame/reg-event-db
 ::http-success
 (fn [db [_ key-path result]]
   (assoc-in db key-path result)))

(re-frame/reg-event-fx
 ::fetch-weather
 (fn [_ _]
   {:http-xhrio
    {:method :get
     :uri    ""
     :params {:lat   (:lat config/home)
              :lon   (:lon config/home)
              :units "imperial"
              :appid config/open-weather-api-key}
     :response-format (ajax/json-response-format {:keywords? true})
     :on-success      [::events/http-success [:weather]]
     :on-failure      [::events/http-fail [:weather]]}}))

roelof13:01:09

but now I get this error message

toy_project1/events.cljs [line 34, col 45] Invalid keyword: ::events/http-success.

roelof13:01:41

if I add events to the require I see a circular dependency error

p-himik13:01:39

Either resolve the circular dependency error by extracting common functionality into a separate namespace or use a full name of the keyword. So if a is an alias for namespace foo.a, then you can use :foo.a/keyword instead of ::a/keyword - without having to require foo.a.

p-himik13:01:46

CLJ 1.11 will allow you to alias namespaces for usage with keywords without actually loading those namespaces. And it's likely that it'll soon become available in CLJS as well.

roelof13:01:48

I need a break

----- ERROR -------------------------------------------------------------------
 File: /home/roelof/reagent-projects/src/toy_project1/events.cljs:34:58
--------------------------------------------------------------------------------
  31 |               :units "imperial"
  32 |               :appid config/open-weather-api-key}
  33 |      :response-format (ajax/json-response-format {:keywords? true})
  34 |      :on-success      [::toy-project1.events/http-success [:weather]]
----------------------------------------------------------------^---------------
toy_project1/events.cljs [line 34, col 58] Invalid keyword: ::toy-project1.events/http-success.

p-himik13:01:19

Same exact error as before - the alias is not available.

roelof13:01:50

found it I needed one : instead of two ::

p-himik13:01:54

An advice - start the compiler's watch when your project is buildable (assuming, you left it that way), wait for it to finish, and then start introducing gradual changes. Don't write hundreds of lines of code and then build it all at once. The more gradual you are, the easier it is to notice such errors in a timely manner and fix them right then and there.

roelof13:01:14

I do that already

👍 1
roelof13:01:15

thanks for the help

p-himik13:01:59

No problem.

roelof13:01:33

learned a few things on the road to learning react

roelof13:01:43

but still a lot to learn

thheller19:01:32

you are in the toy-project1.events namespace so to do it properly you just do ::http-success

👍 1
thheller19:01:06

:: uses the current namespace as the alias so that becomes :toy-project1.events/http-success. if you are in another file you need a require with :as and use that alias to get the same one