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
__dirnameis undefined (in any file) when using LiveView. When LiveView is not used,__dirnameworks 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*var win = Ti.UI.createWindow({ backgroundColor: '#fff' }); var btn1 = Ti.UI.createButton({ title: 'Show __dirname and __filename', top: 100 }); btn1.addEventListener('click', function() { alert('__dirname: ' + __dirname + '\n__filename: ' + __filename); }); var btn2 = Ti.UI.createButton({ title: 'require() other file', top: 200 }); btn2.addEventListener('click', function() { var test = require('./dir/test'); test.showProps(); }); win.add(btn1); win.add(btn2); win.open();[~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:exports.showProps = function () { alert('__dirname: ' + __dirname + '\n__filename: ' + __filename); };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\_\_dirnameand\_\_filenamein 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__dirnameand__filenamewithout any issues. *FR Passed (Test Steps)*Created a new titanium application
Created a
dirdirectory in theResourcesfolder and named ittest.jsCopied the above code in to the files
Ran the program
Pressed
Show __dirname and __filenamebuttonShow __dirname and __filenamevalues were returnedPressed
require() other filebutton#
*Test Environment*Show __dirname and __filenamevalues were returned