Fork me on GitHub
#shadow-cljs
<
2021-12-08
>
martinklepsch10:12:16

I have a script build with :js-options {:js-package-dirs ["functions/node_modules"]} but when I run the script it can’t find modules that only exist in functions/node_modules . I tried running the script from the functions directory and also tried compile and release (simple) builds.

martinklepsch11:12:31

The directory structure looks like this • scripts/index.jsfunctions/node_modulesshadow-cljs.edn

thheller16:12:39

well yeah the output should be functions/index.js

thheller16:12:57

I assume this is node in which case node resolve rules apply

thheller16:12:26

remember that node build targets by default do not bundle their dependencies. so they need to be available at runtime using node resolve rules. in short that is <script-dir>/node_modules and <script-dir>/../node_modules and so on going up

thheller16:12:00

if you want a fully self contained script that has no outside dependencies I recommend post processing with https://github.com/vercel/ncc

thheller16:12:28

shadow-cljs can bundle deps too but all of it is optimized for the browser so ncc generally works better for node builds

thheller16:12:19

but in general I would just recommend putting the :output-to into the dir that also has the node_modules dir

martinklepsch16:12:10

gotcha, yeah that makes sense then. I’ll try to just move the file into functions . I did play with ncc at some point which also worked pretty well but not really required in this case.

martinklepsch16:12:24

I somehow thought that the :js-package-dirs option would magically tell the compiled script where to find packages but I think it’s slightly different / only has that effect for browser builds

thheller16:12:58

yeah it only telle shadow-cljs where to find packages when bundling (which node targets don't do by default)

martinklepsch16:12:44

cool, it’s still used for node though is it? like for compile time warnings or stuff like that?

thheller16:12:26

only for cases where you do (:require [react]) or any kind of symbol where shadow-cljs needs to verify you mean a npm package

thheller16:12:51

but no it never looks at actual JS sources from node_modules for node builds

thheller16:12:12

can't look at stuff like fs and so on anyways given they are built-in