document
CommonJS modules and require(id)
A .js file extension is always added to id, e.g. require("./foo") will
load the file ./foo.js and return its exports object.An id is relative (to the script which require'd it) if it starts with
./ or ../. Otherwise, it's considered a "top-level id" (CommonJS
term).Top level id is evaluated as absolute filesystem path if possible
(e.g. /x/y or ~/x). Otherwise, it's searched at scripts/modules.js/ in
mpv config dirs - in normal config search order. E.g. require("x") is
searched as file x.js at those dirs, and id foo/x is searched as file
foo/x.js.
$ tree $HOME/.config/mpv/scripts/mpv-tools
$HOME/.config/mpv/scripts/mpv-tools
βββ Blackbox.js
βββ Colorbox.js
βββ Gallerizer.js
βββ Leapfrog.js
βββ auto-keep-gui-open.lua
βββ cycle-video-rotate.lua
βββ modules.js <-- How to add "$HOME/.config/mpv/scripts/mpv-tools/modules.js" to js module search path?
βΒ Β βββ AssFormat.js
βΒ Β βββ MicroUtils.js
βΒ Β βββ Options.js
βΒ Β βββ PathIndex.js
βΒ Β βββ PathTools.js
βΒ Β βββ PlaylistManager.js
βΒ Β βββ PseudoRandom.js
βΒ Β βββ RandomCycle.js
βΒ Β βββ SelectionMenu.js
βΒ Β βββ Stack.js
βββ multi-command-if.lua
βββ quick-scale.lua
Currently the modules search path is fixed to the config dirs, and it's not possible to add custom search paths.
What's wrong with creating mpv-tools dir inside scripts/modules.js and then e.g. require ("mpv-tools/PathTools") ? - though if these files require eachother then they'd need to change their require path too.
EDIT - for the proposed solution, IIRC the mpv-tools modules can simply require eachother with e.g. require("./PathTools"), i.e. they load a module relative to their own dir - and in the dir they reside at. It still requires changing these files, but then you can place them at any dir and they'll still know to require eachother correctly, and only the main "client" require will need to use their path, like require ("mpv-tools/ThingToRequire").
You can also try overriding the require function so that if the module id doesn't start with a . or / it looks inside certain directories.
You can also try overriding the require function
While possible, it's not trivial, and that would have to account for modules which require other modules too, as there are multiple instances of the require function (there's one global, and one for each loaded module - with its own base path so that relative require would work also for modules at any path).
Ah, okay. I didn't know that. In my setup, I just have a bootstrap script that loads all the other scripts.
Thanks for the replies, I would fix all up-stream scripts require("blah") function to make it relative require("./blah").
FWIW, I have a prototype which allows changing the modules search dirs via module.paths - currently singletone shared by all the require instances, and which the main script can modify too. Still not sure if it's worth adding, but I might.
It's now possible to change the paths via mp.module_paths. See https://github.com/mpv-player/mpv/commit/4fc5cd32d022fbc898052bbc5beda2e618137aa4
Most helpful comment
It's now possible to change the paths via
mp.module_paths. See https://github.com/mpv-player/mpv/commit/4fc5cd32d022fbc898052bbc5beda2e618137aa4