Titanium JIRA Archive
Titanium SDK/CLI (TIMOB)

[TIMOB-23264] Windows: Unable to load CommonJS module with no package.json or index.js

GitHub Issuen/a
TypeBug
PriorityHigh
StatusClosed
ResolutionFixed
Resolution Date2016-05-23T13:24:11.000+0000
Affected Version/sRelease 5.4.0
Fix Version/sRelease 5.4.0
ComponentsWindows
Labelsn/a
ReporterRene Pot
AssigneeChristopher Williams
Created2016-04-21T15:16:17.000+0000
Updated2016-06-27T23:54:04.000+0000

Description

When trying to load a CommonJS module (in my case http://github.com/Topener/To.ImageCache) it complained it couldn't find it even though it works on Android/iOS I had to move the js file to the lib folder for it to work again. We need to support the legacy pattern of looking for moduleid/moduleid.js in Titanium CommonJS modules. Right now we require spec-compliant CommonJS directories: a package.json pointing to main; an index.js or an index.json.

Comments

  1. Sharif AbuDarda 2016-04-21

    Hello, According to the link here http://docs.appcelerator.com/platform/latest/#!/guide/Alloy_Controllers-section-34636384_AlloyControllers-LibraryCodeandCommonJSModules in section "Library Code and CommonJS Modules" "Some JavaScript code might not be suitable as controller code, since it does not have an associated view, or you want to separate it from the MVC framework for easier reusability. Create a folder called lib in the app directory of your Alloy project. Add your CommonJS modules or JavaScript code using the CommonJS format into the lib folder. These files are copied to the Resources folder, when compiling your Alloy project. To use the library or CommonJS module, require it with the library name or module name without the 'app/lib' path and '.js' extension". This clearly stated that you add your CommonJS modules or JavaScript code using the CommonJS format into the lib folder. This should clear your concern. Hope this helps. Regards, Sharif.
  2. Rene Pot 2016-04-21

    Does not clear my concern I'm afraid. It causes confusing and doesn't work like iOS and Android work, and especially not since commonjs modules are actually stimulated.
  3. Christopher Williams 2016-04-21

    [~topener] So you had the commonjs module installed in the project or globally? And just did a require('To.ImageCache'); and that failed?
  4. Rene Pot 2016-04-21

    Yes, it is installed as a commonjs module for both iOS and Android through tiapp.xml as a commonjs module. The require is indeed what I use and Windows phone says it cannot find the module. By putting the file in the lib it works... but since I want a universal app, I want a universal configuration through modules!
  5. Christopher Williams 2016-05-02

    [~topener] So it looks like this is a result of Windows being more spec-compliant with regards to CommonJS modules. When we require a directory (which is what we're doing for commonjs modules) we look for a package.json, then an index.js, then an index.json. I believe the legacy code for iOS/Android explicitly look for Titanium modules to have a JS file with the module-id.js name inside the folder. So a couple things here: - An easy workaround for you is to add a package.json like so:
       
       {
           "description": "To.ImageCache",
           "directories": {
               "lib": "."
           },
           "main": "To.ImageCache",
           "name": "To.ImageCache",
           "version": "0.1"
       }
       
    - We should likely have some special logic in Windows to try the fallback legacy way of loading a commonjs module for Titanium modules (look for "module-id.js").
  6. Christopher Williams 2016-05-20

    https://github.com/appcelerator/titanium_mobile_windows/pull/710
  7. Ewan Harris 2016-06-27

    Verified using: Windows 10 Pro Ti SDK: 5.4.0.v20160627150129 Appc NPM: 4.2.7-2 Appc CLI: 5.4.0-18 Appc Studio: 4.7.0.201606220541 A CommonJS module that has no package.json or index.js can now be used, tested using the module from description https://github.com/Topener/To.ImageCache. Closing ticket.

JSON Source