Hacker News new | past | comments | ask | show | jobs | submit login
Clojure Scripting on Node.js (github.com/babashka)
143 points by reil_convnet on Aug 5, 2022 | hide | past | favorite | 33 comments



Although I have not used nbb, babashka itself has been super useful for avoiding the slower JVM startup time while still using Clojure to automate some tasks at the command line.

Yet, despite many projects using bb + a bb.edn tasks file as a replacement for Make, I find myself still reaching for Make, out of familiarity at least, perhaps to avoid the extra dependency on bb itself?


Author here. Ask me anything on nbb.


What's the difference between this and Clojurescript?


The main difference is that nbb lets you execute a .cljs file straight away, without a compilation step. As such, it doesn't need any JVM tooling. You can create an AWS Lambda with only a couple of lines of code:

https://blog.michielborkent.nl/aws-lambda-nbb.html


What are the language differences. What's missing in SCI's subset? Just Java stuff like deftype?


Multimethods are fully supported. Protocols, defrecord and deftype are supported with some caveats.


What kind of differences/limitations are there when interpreting Clojurescript (nbb) as opposed to compiling it (shadow-cljs, lein, self-hosted?, other things?)?


I love babashka but always find nbb scripts hard because everything returns a promise which makes the normal REPL workflow tricky.. maybe I’m doing it wrong though


I'm not sure if this applies to nbb, but there's a command line flag for the node repl (`--experimental-repl-await`) which allows you to use await in the repl, allowing you to kind of sidestep the normal annoyance of handling promises.


Nbb has nbb.core/await for top level await which is handy for REPL usage. Furthermore it has promesa which is a library for dealing with promises.


Just as an FYI for the interested, there is a self-hosted version of clojurescript now, it enables macros in the same runtime with some caveats but the main downside (AFAIK) is increased bundle size, which is less relevant for node.js.


There was a self-hosted CLJS project for Node.js which is now abandoned. It was called lumo. It came with a custom image of Node.js with lumo built-in for better startup times since the JS bundle of self-hosted CLJS is quite large resulting in not so great startup out of the box. Nbb is just a library that you can use with any Node.js version. That said, it might be worth revisiting the self-hosted CLJS approach at some point.


But the slow-starting, big-bundle default one still works right? For someone developing an electron app maybe it would be a useful option.


I am not the developer of this library. I just have been using it for a small project and thought others might find it interesting as well.


What is this? What is Clojure and why do you need it in node?


Clojure/Clojurescript is a functional lisp dialect and therefore brings you more and other ways to write programs, since it defers much from javascript/typescript. But I think another question would be "why do you need nbb for clojurescript?". The minimal answer is: clojure has slow startup time, and clojurescript need some tooling to produce runnable code. With nbb (or bb for clojure) you can run Clojurescript as a scripting language in the terminal, like python or shell, and still use some libraries both from the nodejs or clojure(-script) ecosystem.


Author here. Nbb is a scripting environment for Clojure running on Node.js. Clojure is a Lisp that emphasises functional and data oriented programming. Scripts are executed using an interpreter, so there is no build/compile step necessary, which is convenient for small applications.

If you're more curious, check out the README which has a list of videos and articles on this.


Clojure is a lisp dialect (One of the most loved languages according to surveys). You can use nbb to write clojure instead of js in Node.


https://tryclojure.org/ in case you want to try it!


Clojure is a lisp on the jvm. javascript compilers exist because people wanted to use clojure in web related projects.


It's not a javascript compiler as Clojure is a hosted language.


Nbb isn't a compiler, but a Clojure interpreter. Clojure is a hosted language, but can be compiled or interpreted. The ClojureScript compiler compiles Clojure to JavaScript, but nbb only interprets Clojure.


Can anyone explain what a hosted language is?


Basically, you take a programming language and make it work on a platform that meant to be programmed using a different PL. Clojure is hosted by design - it's not Java, but can be used to program for JVM. It ain't Javascript, yet can be used to target nodejs and browser; not an [official] CLR language, but you can write .Net programs. You can use Clojure to make Flutter apps with ClojureDart. You can integrate Python into Clojure with libpython-clj. Or write Clojure to target Erlang/OTP; or Rust; or R; There's even a clojure-like language for Lua - Fennel.

There's something about Clojure people like so much, they want it to work atop any platform.

https://github.com/Tensegritics/ClojureDart

https://github.com/clj-python/libpython-clj

https://github.com/clojerl/clojerl

https://github.com/clojure-rs/ClojureRS

https://github.com/scicloj/clojisr

https://fennel-lang.org


Compiles/transpiles into code that runs on a "host" language execution environment (nodejs, JVM, CLR) instead of it's own VM implementation, or native code.


clojurescript absolutely compiles to javascript, as per the official docs:

https://clojure.org/about/clojurescript


But that's not what nbb is.


Clojure is a lisp like language hosted on top of the JVM. By running on top of node you get a faster startup which is desirable for short lived apps.


Is this similar to Lumo (https://github.com/anmonteiro/lumo)? I would assume Lumo has a faster start time since it boots from a V8 snapshot, but otherwise I'd assume the two projects are similar (except that, of course, lumo is EOL)


Nbb still has faster startup time with any system-installed Node.js than lumo with its v8 snapshot.


If anyone is interested in mixing other languages in node, check out this silly little library I threw together: https://www.npmjs.com/package/polyglot-tag

Embed any scripting language in a Node.js template literal!


This is actually really nifty. However can you change it to be asynchronous instead? Right now the polyglot runs synchronously.

What about the stdout/stderr... It ultimately has to give you back a child process. Could there be a spawn version that gives you the child process too?


Ha ha you mean like this: https://www.npmjs.com/package/pipe-tag




Consider applying for YC's Spring batch! Applications are open till Feb 11.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: