I have a Node CLI program with some dependencies and various of its own modules imported via relative file paths. I would like for the code to be able to read its own package.json
file, but I can't figure out how to "find" it when the program is installed (that is, when its package is installed with npm install
or npm install -g
).
Reading and parsing package.json
is easy — when running it from its own source directory. Of course that's true, because the file path "./package.json" works.
I don't mind reading the file and parsing it, but I don't know of a mechanism to resolve "."-relative paths that works the same way as
import foo from "./lib/foo.mjs";
works. Such imports work when the program (well the package) has been installed anywhere on the system if the proper node_modules/.bin
directory is in my PATH, regardless of the working directory I'm using at the time.
Maybe this is impossible, but wanting to load package.json
doesn't seem that weird.