Target nodejs requires based off CWD instead of imported file

clojurescript

#1

I have a script
clojure -m cljs.main -t node -O ‘none’ -d ‘npm’ -o ‘pluto.js’ -co ’ {:asset-path “npm”}’ -c pluto.js

Which compiles successfully. My job is to find a way to publish the resulting pluto.js file to npm alongside the lib/ for people to import it into their projects.

My problem is that I can’t seem to figure out the proper configuration for pluto.js to load its dependencies, located in lib/, with a path relative to itself. It uses paths relative to the calling directory, so if I’m not in the project’s root (e.g. I’ve require(‘pluto.js’) from another folder or after npm installing it), I get an error like:
Error: Cannot find module
‘/home/me/Documents/x/y/ROOT/OTHERDIR/npm/goog/bootstrap/nodejs.js’

The part ‘npm/goog/bootstrap/nodejs.js’ is always appended to $cwd, so the only way it’ll import correctly is if I’m in the project’s root.

I can fix this problem manually, it’s very tedious, by changing the require statements in pluto.js from
require(path.join(path.resolve("."),“npm”,“goog”,“bootstrap”,“nodejs.js”));

To something like
require(“npm/goog/bootstrao/nodejs.js”)

I’m tempted to just sed stuff but that’s a temporary fix.

Any suggestions or pointers? I’ll be happy to provide more details if necessary :slight_smile: