Description
*This is not a regression occurs using 5.5.1.GA stack*
When using
__dirname
in an app.js file the following is thrown
*Using __dirname in any file other than app.js works*
[ERROR] : Application Error: {
[ERROR] : "line": 2,
[ERROR] : "column": 10,
[ERROR] : "message": "require: Error while require(/app) Can't find variable: __dirname",
[ERROR] : "native_stack": [
[ERROR] : "JSExportClass<class Titanium::GlobalObject>::CallNamedFunction"
[ERROR] : ]
[ERROR] : }
Steps to reproduce
In your app.js add console.log(__dirname)
Build for Android, iOS or Windows
Actual result
The above error is thrown
Expected result
__dirname should be able to be used in app.js file
[~cng] I am guessing this is the same issue, but
__dirname
is undefined (in any file) when using LiveView. When LiveView is not used,__dirname
works as expected.PR for Windows: https://github.com/appcelerator/titanium_mobile_windows/pull/903
Fixed for Windows: https://github.com/appcelerator/titanium_mobile_windows/pull/903
PR: https://github.com/appcelerator/titanium_mobile/pull/10241 Test-Case (use a classic "app.js" and create a "test.js" in a directory named "dir"): *app.js*
*/dir/test.js*
[~jquick] Are we good on Android already? The current unit-test assumes that. *EDIT*: Android does not seem to handle this so far. I'll check, but it may require some more attention from the Android team. I assumed this could work, but it does not:
Hmm... interesting. In C/C\+\+, the equivalent would be done via macros. For example, the C/C\+\+
\_\_FILE\_\_
preprocessor would be substituted with a string referencing the source file's path. So, string substitution of\_\_dirname
and\_\_filename
in JavaScript would be an alternative solution when doing an app build, but would be problematic for LiveView. However, if there was a JavaScript equivalent of the C/C\+\+\_\_LINE\_\_
macro for line numbers, then string substitution would be the best solution if JS transpiling was involved. If we were to do this natively on Android, then I believe we override therequire()
method in our "module.js" below. We should be able to set the properties per module there... or in 1 of the JS files in that directory. (I've never mucked with this part of the code, but I believe this is where the magic happens.) https://github.com/appcelerator/titanium_mobile/blob/master/android/runtime/common/src/js/module.js*Closing ticket.* Verified fix in SDK version
7.5.0.v20180920040518
. Able to use__dirname
and__filename
without any issues. *FR Passed (Test Steps)*Created a new titanium application
Created a
dir
directory in theResources
folder and named ittest.js
Copied the above code in to the files
Ran the program
Pressed
Show __dirname and __filename
buttonShow __dirname and __filename
values were returnedPressed
require() other file
button#
*Test Environment*Show __dirname and __filename
values were returned