I am using nbb to give me a nicer way to interact with an existing nodejs project. I have found this way of reloading local js things (example in thread), but it's a bit ugly. Is there a way to make the ns :require reload when it's run?
(ns foo
;; This works, but I'm not able to reload the file when it changes
(:require ["./myproj/foo" :as foo]))
;; can call fns like this
(foo/someFn 123)
(defn unrequire [path]
(js-delete js/require.cache (js/require.resolve path)))
(defn js-require [path]
(unrequire path)
(js/require path))
;; This works and reloads the file when run
(def bar (js-require "./myproj/bar"))
;; but it can't be called like bar/someFn, it has to be called like this
(bar.someFn 123)sure! there's an issue for reloading here: https://github.com/babashka/nbb/issues/343 I think @eval2020 was working on this but if it stalled, I can take a look
Thanks for the incredibly quick response! I'm not sure it's exactly the same thing, or at least my specific use-case isn't mentioned, which is to make local js files reloadable (the issue talks about cljs namespaces).
yeah, it's not the same but related. another issue welcome!
Cool, I will make an issue
I'm looking at JS reloading now. Meanwhile can you try if you can get nbb working locally? Then you could test it
1. clone repo
2. bb dev
3. run ./cli.js ... instead of nbb ....
sure!
got it working (I needed to also run npm i)
I think I have a fix here:
https://github.com/babashka/nbb/commit/f4caba3fafd4be89e30ba706e1a2324b969f8394
branch js-reload
if possible, please test it out
you'll also need commit 9c8ac68687c93b86651e9fdba1f5e720a1e9b582
I doesn't seem to work on the node project I have
It also looks like this broke requiring ["node:fs" :as fs] (but not ["lodash$default" :as _] )
Is your project open source or can you make a repro?
It's not open source, I can make a repro
I'll only be able to do so a bit later though...
ok, FWIW, this worked for me locally with a JS script that prints "dude":
$ ./cli.js -e "(require '[\"./scratch.mjs\" :as fs] :reload) (ns foo (:require [\"./scratch.mjs\" :as fs] :reload))"
dude
dudeyou're right that it breaks node:fs:
$ ./cli.js -e "(require '[\"node:fs\" :as fs] :reload)"
----- Error --------------------------------------
Message: No such built-in module: node:fs?uuid=d5a10e94-b441-4725-89f7-ca99f11726ccthe uuid is appended to force re-evaluation of ES6 modules, but I could do that only when it's a file
pushed a fix for node:fs etc
feel free to poke me to get back tot this, if you have found time to make a repro of some kind