4

In clojure, I can use the plugin lein-exec for lein to "execute" a single clojure file as a script like this:

lein exec foo.clj

Naturally I am also curious whether there is a similar way to "run" a single clojurescript file on node.js directly without the process of compiling and run the .js file(possibly in a terminal)?

Thanks!

2 Answers 2

1

Clojurescript needs to be compiled to JS if you want to execute it. That I know of there is no direct invocation method from lein cljsbuild since CLJS executes in multiple environments (node, rhino, v8, browser, etc) and each is very specific about how to run the JS, so it can't make the choice for you.

Here are a couple of quick ways of running a cljs script in node (keep in mind that the compilation will take time since it is java based and the startup time is a bit bad)

The easy way

The easiest way right now would be something like this (for executing an script in node):

lein new mies-node hello-world
cd hello-world
lein cljsbuild once
node run.js

There are more templates for node cljs, mies-node is just one of them.


The hard (but later more immediate way)

If you want the hard way, install the clojurescript compiler and then you can do something like this:

$ node -e "$(cljsc hello.cljs '{:target :nodejs :optimizations :whitespace}')"

Note: cljsc will need to be in path

Note: this may give you problems with externs and what not. play with the options you pass to the compiler

Sign up to request clarification or add additional context in comments.

Comments

0

Because your question was: "to run a single clojurescript file", then there's an attempt to make Clojurescript compiler self-host:

https://github.com/lazerwalker/clojurescript-in-clojurescript

the Readme says:

./run.js nodecljs.js hello.cljs

In case you want to run the Nodejs-backed REPL:

https://github.com/bodil/cljs-noderepl

The "problem" (for your concern) with Clojurescript is that it uses Google Closure library (the goog.require thingies) to manage module (aka namespace in Clojurescript term) dependencies which isn't Nodejs' style for loading modules. Hence all involved modules need to be packed into a single javascript file (using any optimization modes: :whitespace, :simple or :advanced) to be able to run in Nodejs.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.