Can't load native node module with cljs.main tool

I try to scripting using new cljs.main tool

deps.edn:

{:deps {org.clojure/clojure {:mvn/version "1.9.0"}}

 :aliases
 {:cljs {:main-opts ["-m" "cljs.main" "-re" "node"]
         :extra-deps {org.clojure/clojurescript {:mvn/version "1.10.238"}}}}}

test.cljs

(require 'fs)
(println (fs/readFileSync "README.md" "utf8"))

When running with cmd: clj -A:cljs test.cljs it throws exception:

Exception in thread "main" clojure.lang.ExceptionInfo: No such namespace: fs, could not locate fs.cljs, fs.cljc, or JavaScript source providing "fs" at line 1 test.cljs..

But when I try with other node module, e.g. install left-pad & test with code bellow, it just work fine.

(require 'left-pad)
(println (left-pad "foo" 5))

So I’m not sure what’s thing I miss here?

**update: run 2 scripts above on cljs REPL directly & it work fine. So I don’t know what’s issue with clj -A:cljs test.cljs cmd

Probably is because how you are requiring the package. Fs is a native node module, ie, you don’t install it. This means that it does not exist on the node modules folder. Try to require it using native node require method

yep, (defonce fs (js/require "fs")) is work fine.

But I think (require 'fs) is supposed to work as well? (I can run it from cljs REPL, start by clj -A:cljs, without error).

Just seems like a bug, I filed https://dev.clojure.org/jira/browse/CLJS-2724

2 Likes