Inconsistent behaviour using require to load local .js files


#1

I’m using a library in a js file located at app\utils\mylib.js
In my codebehind I load it by using “require(‘utils/mylib’)” this works like this if this module is the starting point of the app
ex:
app.start({ moduleName: ‘views/main-page/main-page’ });
But, if I start from a different module (a login page for example) and change to main-page module, then I get the exception "Failed to find module… relative to …"
What happens here is that the first module started seems to load the required file relative to the app directory, while on the other case, it tries to load it relative from the module directory. Wich in this case whould need to require(’./…/…/utils/mylib’)

Is this a desired behaviour? Is there any way to work around this?


#2

NativeScript implements node.js’s modules specification (commonjs) - https://nodejs.org/docs/latest/api/modules.html#modules_all_together

Having said that - using require(“lib”) without any of the following prefixes / ./ ~/ ../ will try to resolve a module located in node_modules, or (app/tns_modules/ in terms of on-device directory structure).

I’d be happy to debug a project pushed to a github repo if this proves to indeed be an issue.