nbb

2024-08-23T18:13:59.478539Z

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?

2024-08-23T18:14:22.078869Z

(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)

borkdude 2024-08-23T18:15:33.297899Z

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

2024-08-23T18:29:08.797909Z

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).

borkdude 2024-08-23T18:29:28.591039Z

yeah, it's not the same but related. another issue welcome!

2024-08-23T18:29:53.802429Z

Cool, I will make an issue

borkdude 2024-08-23T18:33:19.685169Z

I'm looking at JS reloading now. Meanwhile can you try if you can get nbb working locally? Then you could test it

borkdude 2024-08-23T18:33:42.244159Z

1. clone repo 2. bb dev 3. run ./cli.js ... instead of nbb ....

2024-08-23T18:33:51.711219Z

sure!

2024-08-23T18:38:53.616449Z

got it working (I needed to also run npm i)

borkdude 2024-08-23T19:26:08.794559Z

I think I have a fix here: https://github.com/babashka/nbb/commit/f4caba3fafd4be89e30ba706e1a2324b969f8394 branch js-reload

borkdude 2024-08-23T19:26:14.920079Z

if possible, please test it out

borkdude 2024-08-23T19:34:04.964849Z

you'll also need commit 9c8ac68687c93b86651e9fdba1f5e720a1e9b582

👍 1
2024-08-23T20:02:06.613029Z

I doesn't seem to work on the node project I have

2024-08-23T20:10:49.809219Z

It also looks like this broke requiring ["node:fs" :as fs] (but not ["lodash$default" :as _] )

borkdude 2024-08-23T20:11:25.174499Z

Is your project open source or can you make a repro?

2024-08-23T20:11:48.325869Z

It's not open source, I can make a repro

2024-08-23T20:12:43.199569Z

I'll only be able to do so a bit later though...

borkdude 2024-08-23T20:54:28.324829Z

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
dude

borkdude 2024-08-23T20:55:39.263079Z

you'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-ca99f11726cc

borkdude 2024-08-23T20:56:18.376749Z

the uuid is appended to force re-evaluation of ES6 modules, but I could do that only when it's a file

borkdude 2024-08-23T21:01:25.626969Z

pushed a fix for node:fs etc

borkdude 2024-08-26T12:03:42.024159Z

feel free to poke me to get back tot this, if you have found time to make a repro of some kind