Description
Background: I'm working on a VSCode extension for Espruino.
The localModules file assumes that the source code is in the process.cwd()
, which isn't always true. For my scenario the cwd is completely different from the file location, since the vscode window runs somewhere else entirely. It would be nice to be able to set the value of cwd somehow in the module.
There is also a problem with the loadEspModules
function, which uses (essentially) fs.readFileSync(path.join('modules', moduleName))
, which ends up with an implicit process.cwd().
I also don't think it behaves like require()
should do, since it always loads it relative to the current process, not the file that requires it. For example, given that main.js: requires('./folder/file.js')
and folder/file.js: requires('./anotherFile.js)
, then it should find folder/anotherFile.js
, but since it doesn't use the current location of the file that required it, it ends up looking in the root folder. For this to work we need to know the path of the current module when it looks for the next module. It would then have to rewrite the code so that the url is absolute, for running on the device. This is a bit more complex to solve, and would have to involve the modules.js
file too. The above changes can be done in localModules.js
only.