Titanium JIRA Archive
Titanium SDK/CLI (TIMOB)

[TIMOB-23995] iOS: Using __dirname in app.js errors with Can't find variable: __dirname

GitHub Issuen/a
TypeBug
PriorityHigh
StatusClosed
ResolutionFixed
Resolution Date2018-08-10T13:06:53.000+0000
Affected Version/sRelease 6.0.0, Release 5.5.1
Fix Version/sRelease 7.5.0
ComponentsAndroid, iOS
Labelsqe-6.0.0
ReporterEwan Harris
AssigneeHans Knöchel
Created2016-10-06T16:31:46.000+0000
Updated2018-09-24T14:16:17.000+0000

Description

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

Comments

  1. Brenton House 2016-11-23

    [~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.
  2. Kota Iguchi 2016-12-19

    PR for Windows: https://github.com/appcelerator/titanium_mobile_windows/pull/903
  3. Kota Iguchi 2016-12-20

    Fixed for Windows: https://github.com/appcelerator/titanium_mobile_windows/pull/903
  4. Hans Knöchel 2018-08-09

    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*
       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();
       
    */dir/test.js*
       exports.showProps = function () {
           alert('__dirname: ' + __dirname + '\n__filename: ' + __filename);
       };
       
    [~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:
       kroll->Set(NEW_SYMBOL(isolate, "__dirname"), STRING_NEW(isolate, "/"));
       kroll->Set(NEW_SYMBOL(isolate, "__filename"), STRING_NEW(isolate, "/app.js"));
       
  5. Joshua Quick 2018-08-09

    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 the require() 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
  6. Samir Mohammed 2018-09-24

    *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 the Resources folder and named it test.js

    Copied the above code in to the files

    Ran the program

    Pressed Show __dirname and __filename button

    Show __dirname and __filename values were returned

    Pressed require() other file button

    # Show __dirname and __filename values were returned

    *Test Environment*
       APPC Studio: 5.1.0.201808080937
       iPhone 6 (12.0)
       APPC CLI: 7.0.7-master.1
       Operating System Name: Mac OS High Sierra
       Operating System Version: 10.13.6
       Node.js Version: 8.9.1
       Xcode 10.0
       

JSON Source