Titanium JIRA Archive
Titanium SDK/CLI (TIMOB)

[TIMOB-23994] Android: Using __filename in app.js errors with Can't find variable: __filename

GitHub Issuen/a
TypeBug
PriorityMedium
StatusClosed
ResolutionFixed
Resolution Date2018-08-24T01:48:38.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-06T15:30:31.000+0000
Updated2018-08-24T01:48:42.000+0000

Description

Description

*This is not a regression occurs using 5.5.1.GA stack* When using __filename in an app.js file the following is thrown *Using __filename 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: __filename",
[ERROR] :    "native_stack": [
[ERROR] :      "JSExportClass<class Titanium::GlobalObject>::CallNamedFunction"
[ERROR] :    ]
[ERROR] :  }

Steps to reproduce

In your app.js add console.log(__filename)

Build for Android, iOS or Windows

Actual result

The above error is thrown

Expected result

__filename should be able to be used in app.js file

Comments

  1. Kota Iguchi 2016-12-20

    Fixed for Windows: https://github.com/appcelerator/titanium_mobile_windows/pull/903
  2. Hans Knöchel 2017-03-14

    On iOS, the __filename property is injected inside the CommonJS scope. Example: app.js
       var win = Ti.UI.createWindow({
           backgroundColor: '#fff'
       });
       
       var btn = Ti.UI.createButton({
           title: 'Trigger'
       });
       
       btn.addEventListener('click', function() {
           var test = require('/test');
       });
       
       win.add(btn);
       win.open();
       
    test.js:
       alert(__filename);
       
    This will work. It won't work on the app.js. [~cwilliams] Is this an intended behavior?
  3. Eric Merriman 2017-04-10

    Moving to post 6.1.0.
  4. Hans Knöchel 2018-08-09

    PR: https://github.com/appcelerator/titanium_mobile/pull/10247
  5. Samir Mohammed 2018-08-14

    *Closing ticket.* Verified fix in SDK version: 7.4.0.v20180810061237. Application no longer crashes when using (__filename) or (__dirname). *FR Passed (Test Steps):*

    Created a new application using Titanium

    Added the following in to the app.js

       Ti.API.info(__dirname);
       Ti.API.info(__filename);
       

    Ran the program

    Application no longer showed an error and the following could be seen in the console

       [INFO] :   /
       [INFO] :   /app.js
       
    *Test Environment*
       APPC Studio: 5.1.0.201808080937
       APPC CLI: 7.0.4
       Pixel XL (8.1)
       Operating System Name: Mac OS High Sierra
       Operating System Version: 10.13
       Node.js Version: 8.9.1
       Xcode 9.2
       
  6. Samir Mohammed 2018-08-14

    *Edit* SDK Version: {[7.4.0.v20180813131247}} was used.

JSON Source