Get a list of "RootDirectory" instead of just one ? #enhancement
Hi again caoccao,
Looking for other issues discussions, I found you have oriented your work to get it functional on Android (JDK 1.6 / 8) so I'm not sure about my proper request as I don't know Android (except with gluon) and it's not my main concern...
I like to get things correctly separated, ie. different universes can coexist if they do not interfere in their definition but can interact through gateway, and Javet is a gateway :)
In other words and to focus on the request, I want to be able to get my own scripts in a folder (inside a maven project) and all the other stuffs in the nodeJS world, which means:
/ (root)
/my_folder (somewhere on the file system /external_ssd/.../my_folder)
/src
/main
/javascript
/stuffs.0
a_script.js
another_script.js
main.js (which rely on another_script.js and best case also on a_script.js)
/another_folder_with_nodejs
/node_js
/lib (for '... install -global)
/node_modules
package.json (? not present on my installation as I don't want to make something 'global')
/another_folder_with_temporary_files_or_other
/local_nodejs_dependencies
/node_modules
package.json
I am able to get this works (without external dependencies) if I set RootDirectory as the folder /root/my_folder/src/main/javascript, and thanks to you I can have a script with external dependency working by defining the RootDirectory as /another_folder_with_temporary_files_or_other/local_nodejs_dependency, but the setRootDirectory will override the previous definition so I can't have both 'repository' available, I tried not to setRootDirectory in the case I have no external dependencies but dependencies in the script folder are not found, setting RootDirectory as /root/my_folder/src/main/javascript is mandated...
Basic solution is a loop on "roots" which can imply an order and duplication remains the responsability of the developper where order could be a solution depending on context as we can have multiple instances... And if it can be allowed to get things like this, I will keep setRootDirectory to clear the list and define the existing RootDirectory and add something like:
- setModuleDirectories(String... folders)
- setModuleDirectories(File/Path?... folders) // if we can have same behavior with File as with String, ie /folder not work whereas /folder/ is OK
- addModuleDirectory(String/File/Path? folders) and eventually
- addModuleDirectory(int _index, String/File/Path? folders)
(as I imagine you also have a register to manage "inline script" and other, don't know how difficult it is...)
Well, I'm sure you have the idea ;)
Is it reasonable to consider getting this functionality available ?
If I understood correctly, you'd like to have multiple Node.js runtime to have their individual root directories.
No
The simple answer is: impossible.
The root directory is a per-process concept. One process can only have one root directory. In Javet, all Node.js runtimes are threads in the same process which is JVM. So all the runtimes and JVM itself have only one root directory. That can explain the behavior you described.
I suggest you pack your JS projects altogether and keep only one node_modules.
But, Maybe
The sophisticated answer is: possible.
It's possible to virtualize Node.js while it attempts to retrieve the root directory. That most probably means to hack the Node.js source code. One of the general design principles is Javet doesn't hack either Node.js or V8 so that Javet users get the genuine Node.js or V8.
Hope these make sense. Please let me know if you have any questions.
Hi caoccao,
I agree with you, hacking NodeJS is not the solution :) And this is why I appreciate your work, we stay with a NodeJS reference implementation.
As I didn't deep into Javet code and don't have knowledges yet about nodejs, I just imagine rootDirectory concept could be in Javet environment defined in another way, namely a list to allow what I request.
I also agree that in production mode, this is probably the only correct behavior, so I have to consider making node modules and found how to deploy them easily during development. I will also make the test on mix mode ie deploying node_modules in my source folder to see if it could work just like this with modules and scripts not in a module.
I also think about modularization https://www.caoccao.com/Javet/reference/resource_management/modularization.html
But if I understand correctly, it only works in V8 mode.
And about 'have multiple Node.js runtime to have their individual root directories.', no I just want a single runtime for now with multiple virtual "root"s ;)
Thank you for your answer ;)
Actually, you might want to try the options listed below.
Polyfill
You may polyfill requre and process to virtualize the root directory. The polyfill needs to be backed up by Java and can do almost whatever you want to do.
ESM
ESM also works in Node.js. You may have your own module resolver to load the scripts from anywhere.