Titanium JIRA Archive
Titanium SDK/CLI (TIMOB)

[TIMOB-23382] Parity: Cannot require directory as done in NodeJS

GitHub Issuen/a
TypeBug
PriorityCritical
StatusClosed
ResolutionFixed
Resolution Date2016-06-10T08:07:56.000+0000
Affected Version/sRelease 5.3.0
Fix Version/sRelease 6.0.0
ComponentsAndroid, iOS
Labelsnotable, parity
ReporterChristopher Williams
AssigneeChristopher Williams
Created2016-05-16T17:13:43.000+0000
Updated2018-11-16T15:40:54.000+0000

Description

If you attempt to require a directory, NodeJS has well-defined behavior: https://nodejs.org/api/modules.html#modules_all_together Namely: - try to load dir/package.json and grab the main property, load the file pointed to by that. - try to load dir/index.js as JS - try to load dir/index.json as JSON Trying this on Android and iOS, it just throws a runtime error that the requested module was not found.

Comments

  1. Christopher Williams 2016-05-16

    The NodeJS require algorithm _is_ implemented for Windows SDK.
  2. Christopher Williams 2016-05-17

    https://github.com/appcelerator/titanium_mobile/pull/8004
  3. Chee Kiat Ng 2016-06-10

    Works great!

    Steps to test

    1. *appc new --classic* 2. Obtain this module scapegoat from https://github.com/brentertz/scapegoat 3. put the module in /Resources/scapegoat 4. put this in app.js
       Ti.UI.createWindow().open();
       var scapegoat = require('scapegoat')
             escape = scapegoat.escape,
             unescape = scapegoat.unescape;
       
       var html = '<h1>Hello World</h1>',
             escaped = escape(html),
             unescaped = unescape(escaped);
             
       alert('html: ' + html + '\nescaped: ' + escaped + '\nunescaped: ' + unescaped);
       
    5. *appc run -p ios*

    Expected Result

    An alert will show with this result
       html: <h1>Hello World</h1>
       escaped: <h1>Hello World</h1>
       unescaped: <h1>Hello World</h1>
       
    Repeat with *appc -run -p android* you can get the same result PR MERGED!!
  4. Ashraf Abu 2016-06-10

    Did the same for android. Same results.
       html: <h1>Hello World</h1>
       escaped: <h1>Hello World</h1>
       unescaped: <h1>Hello World</h1>
       
    Works well.
  5. Chee Kiat Ng 2016-06-10

    [~bimmel] probably need to be documented somehow like "hey, Titanium can require directory like what NodeJS does normally now"
  6. (deactived) Brian Immel 2016-06-10

    Thanks for the heads up [~cng]. I'll add this to the docs when we get to 6.0.0 release.
  7. (deactived) Brian Immel 2016-06-10

    Thanks [~cwilliams]. I'll take a look at it and write up a little something and post it back here for review.
  8. (deactived) Brian Immel 2016-06-10

    [~cwilliams] and [~cng], I've written up a first draft of the changes per this ticket: https://wiki.appcelerator.org/display/~bimmel/2016/06/10/Daily+Update+6-10-16#DailyUpdate6-10-16-RequireDirectory Let me know what I got right and wrong. Thanks
  9. Lokesh Choudhary 2016-08-15

    Verified the fix. Used the test case above. Closing. Environment: Appc Studio : 4.7.0.201607250649 Ti SDK : 6.0.0.v20160814220332 Ti CLI : 5.0.9 Alloy : 1.9.1 MAC El Capitan : 10.11.6 Appc NPM : 4.2.7 Appc CLI : 6.0.0-24 Node: 4.4.4 Nexus 6 - Android 6.0.1 iOS simulator: Iphone 6s 9.3

JSON Source