Introducing the Titanium Code Processor

Illustration with connected people ,futuristic technology concept

Today we’re here to talk about an exciting new tool being integrated with the Titanium family of products: the Titanium Code Processor. We’re always striving to help you generate optimal code and kickass mobile apps, so we’re delivering the Code Processor to perform in-depth dynamic analysis of your Titanium Mobile projects to optimize your code and help reduce errors. The Code Processor performs various types of error checking, code conformance, and optimizations. It is composed of a core analysis engine that performs generic analysis, and a plugin architecture used to analyze the results from the core analysis engine.

The core analysis engine is a specialized JavaScript engine that has been designed to work in an offline environment. By performing dynamic analysis, instead of the more traditional static analysis, we are able to obtain significantly more accurate results.

The core analysis engine, by itself, can only report runtime exceptions. Plugins tie into the core analysis engine to do things that are of much more value. There are currently 6 analysis plugins, with more on the way:

  • Analysis Coverage: determines how much of a project was analyzed by the code processor. This is useful for finding dead code as well as debugging the Code Processor itself.
  • Require Finder: determines all modules/files required by the project.
  • Titanium Deprecation Finder: throws a warning anytime a deprecated API is used.
  • Titanium Platform Validator: makes sure that all references to platform specific APIs are not referenced by the wrong platforms and throws an error if they are used incorrectly.
  • Titanium API Usage Finder: finds all Titanium APIs used by the project.
  • Titanium Include Finder: determines all files Ti.include()’d by the project.

To show the power of the code processor, consider the following two buggy files:

The code processor, when run with exception recovery enabled and for iphone, gives the following results:

Here we see that the Code Processor correctly identified that Ti.Platform.Android was being invalidly accessed when compiled for iphone. But what happened next? This would normally throw an exception, but since we have exception recovery enabled, it returns an “unknown” value. Since the code processor doesn’t know if it’s truthy or falsey, it evaluates both branches of the if statement, hence the two exceptions listed for a and b being unknown identifiers.

Sound interesting? To start experimenting with the Code Processor, make sure you have the Titanium CLI installed and then install the code processor using npm:

[sudo] npm install -g titanium-code-processor

To run the code processor:

titanium-code-processor -o [platform] [path-to-project]

For more information and advanced usage options, check out the Code Processor on GitHub. Since this product is still beta, we could really use your help! If you find any problems with the code processor, please let us know at the CLI Google Group. There is also a reporting mechanism built into the code processor itself. By adding the -r flag, the results will be uploaded to ACS for us to review. You can also use the -m flag to add a message to the report.

Update: The command usage information in this blog post is outdated. Please visit the titanium-code-processor homepage on GitHub for the most up-to-date information.


  1. how to fix this:

    throw err;
    Error: Cannot find module ‘request’
    at Function.Module._resolveFilename (module.js:338:15)
    at Function.Module._load (module.js:280:25)
    at Module.require (module.js:362:17)
    at require (module.js:378:17)
    at Object. (/usr/local/Cellar/node/0.8.14/lib/node_modules/titanium-code-processor/bin/codeprocessor:19:12)
    at Module._compile (module.js:449:26)
    at Object.Module._extensions..js (module.js:467:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Module.runMain (module.js:492:10)

  2. egomezMBP:Documents egomez$ titanium-code-processor Titanium Studio Workspace/1BestPractices/ -r -m

    throw err;
    Error: Cannot find module ‘request’
    at Function.Module._resolveFilename (module.js:338:15)
    at Function.Module._load (module.js:280:25)
    at Module.require (module.js:362:17)
    at require (module.js:378:17)
    at Object. (/usr/local/lib/node_modules/titanium-code-processor/bin/codeprocessor:19:12)
    at Module._compile (module.js:449:26)
    at Object.Module._extensions..js (module.js:467:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Module.runMain (module.js:492:10)

    SDK: [INFO] Titanium SDK version: 3.0.0 (10/08/12 21:09 7bf73ff)
    [INFO] Titanium SDK version: 3.1.0 (11/09/12 14:50 9390d4f)

    On tiapp.xml:


    To reproduce: Install, run titanium-code-processor, use foo.js module sample.

  3. @Eduardo

    there is a dependency missing.

    Just run:
    sudo npm install -g request

    and after the install titanium-code-processor should work

  4. Nice job, I know it’s still beta but here’s some feedback for what it’s worth…

    1. It seems like this should be built into the intellisense of the IDE, not a tool that you need to manually launch.

    2. The require finder would be a whole lot more useful if it didn’t find all modules required by the project, but instead showed which module requires are *not* in use in a given file. That way you can remove unused references.

    3. With the json output I could see it very quickly becoming “information overload”, where it’s difficult to get out of the json the relevant warnings that the developer cares about (calling Android API on iOS, etc…) A lot of json was outputted above for a very tiny app, imagine in a real-world app with thousands of lines, the json would be huge. Instead, the IDE should parse that json and show all relevant warnings/errors in a tab at the bottom, which is something that Visual Studio does very well.

  5. Hi,

    Looks like a great project, but always throws an error on start up for me.

    Version 0.1.5 got installed via npm

    Error text is:
    throw e; // process.nextTick error, or ‘error’ event on first tick
    TypeError: Object # has no method ‘existsSync’
    at Object. (/usr/local/lib/node_modules/titanium-code-processor/bin/codeprocessor:143:9)
    at Module._compile (module.js:441:26)
    at Object..js (module.js:459:10)
    at Module.load (module.js:348:31)
    at Function._load (module.js:308:12)
    at Array.0 (module.js:479:10)
    at EventEmitter._tickCallback (node.js:192:40)

    Is there a bug tracker or a git repo we can access and branch from?


  6. @Eduardo It’s fixed now

    @Justin Toth
    1) Eventually we plan to do exactly that. Given the scope and complexity of the project we felt it was best to develop it on its own so that the Studio team didn’t get held up on it.
    2) Finding which modules are required is extremely useful for the build process, which is the real intent of that plugin. That said, we do want to eventually include a dead code removal tool
    3) That’s why it’s outputting JSON actually, so that it’s easy for Titanium Studio to parse and display in a more human friendly format. It might not be a bad idea to have some sort of flag on the built-in bin script to toggle between output modes though.

    The code processor is also integrated into the Titanium CLI as part of the build step, which shows friendlier output.

  7. @Neville That error sounds like you’re running node 0.6 (they moved the existsSync method from path to fs in 0.8). The titanium code processor requires node 0.8. You can double check by running ‘node -v’

    You can use the version from git at although right now it and npm are completely in sync

    To file bugs, use JIRA The code processor is a part of the tooling component of the titanium mobile project.

  8. I am new to node.js, I downloaded the latetst npm, , got Titanium CLI and the code processor, ran titanium-code-processor [path-to-project] – and got

    info: Analyzing project
    { [Error: Command failed: [ERROR] “project” is an unrecognized command.
    ] killed: false, code: 1, signal: null }

    Any tips for me?

  9. @Hannes

    Have you installed a 3.0 Titanium SDK? It’s required for the CLI to function properly. If you haven’t installed one yet, try running “titanium sdk install --branch 3_0_X”

  10. Hi,

    I tried running the code processor and I see that it is processing only app.js.

    I ran the following command in the directory which has tiapp.xml

    $ titanium-code-processor analyze -o iphone

    The output contains parsing results of app.js only though inside my Resources folder I have subfolders having other JS files.

    Am i missing something here?

    Thanks in advance

  11. Hi bdg. There are a variety of reasons that only app.js could be parsed and really it’s dependent on your code. The code processor works by actually executing code, so if code is never executed it is never analyzed.

    This has the implication that if a require statement is never executed, then the file being required is never analyzed. Let’s consider a project with two files: app.js and foo.js. Inside app.js we have the following code:

    if (false) {

    In this case, foo.js will (rightfully) not be processed. Of course there are bugs in the code processor, so it’s possible that the require statements are being ignored unintentionally 🙂

    I would need to see your code to make a more definitive statement on why the other files aren’t being Ti.include()’d or require()’d.

    You can run the code processor with the option “-c processUnvisitedCode=true” that tells the code processor to analyze any code that was missed after the fact. Analysis isn’t as accurate in this case compared to if it was analyzed in the first pass, and it usually tends to find a lot of “false positives” in terms of errors, api usage, code coverage, etc, but it’s still something.

  12. Hi Bryan,

    Thanks for the quick response.

    The code is running on iphone and on android with no change.

    I just went to the project directory which has tiapp.xml and ran the following command:

    $ titanium-code-processor analyze -o iphone

    which parsed only the app.js file. The app.js file is the only file which resides inside Resources folder. All other JS files are inside subfolders in Resources folder.

    I tried your suggestion and executed the following command:
    $titanium-code-processor analyze -c processUnvisitedCode=true -o iphone

    it gave the following output:

    if (!process.listeners(‘uncaughtException’).length) throw e;
    TypeError: Cannot read property ‘className’ of null
    at Object.AST.walk.callback (/usr/local/lib/node_modules/titanium-code-processor/lib/Base.js:8733:35)
    at Object.visit (/usr/local/lib/node_modules/titanium-code-processor/lib/AST.js:145:78)
    at Object.TreeWalker._visit (/usr/local/lib/node_modules/titanium-code-processor/node_modules/uglify-js/lib/ast.js:899:24)
    at AST_Node.DEFNODE._walk (/usr/local/lib/node_modules/titanium-code-processor/node_modules/uglify-js/lib/ast.js:507:24)
    at /usr/local/lib/node_modules/titanium-code-processor/node_modules/uglify-js/lib/ast.js:148:14
    at Array.forEach (native)
    at walk_body (/usr/local/lib/node_modules/titanium-code-processor/node_modules/uglify-js/lib/ast.js:147:20)
    at AST_Node. (/usr/local/lib/node_modules/titanium-code-processor/node_modules/uglify-js/lib/ast.js:159:13)
    at Object.TreeWalker._visit (/usr/local/lib/node_modules/titanium-code-processor/node_modules/uglify-js/lib/ast.js:903:21)
    at AST_Node.DEFNODE._walk (/usr/local/lib/node_modules/titanium-code-processor/node_modules/uglify-js/lib/ast.js:158:24)


    Any pointers? Thanks in advance !

  13. Hmm…that’s definitely a bug in the Code Processor, but without seeing your code I can’t say where unfortunately

    I just realized there was a bug in the readme. Both commands should be run with the “–all-plugins” flag. You might try running the original command, but with this flag added to it, so

    titanium-code-processor analyze -o iphone –all-plugins

    Without that flag, require and Ti.include are disabled

  14. Hi Bryan,

    Is it possible to send me some sample code on which I can try the tool and verify if my setup is correct?


  15. Hi bdg,

    I’m sorry I’m not really sure what you mean by sample code to verify your setup. You can run any code in the code processor. If you want to analyze something basic, you can always run the default two-tab app which generates no errors or warnings.

  16. Hi Bryan,
    when i try to analyze a project (also the two-tab app) i only have “An exception was thrown but not caught: Cannot dereference Titanium of undefined” in app.js rows, how can i solve it and analyze the rest of the project’s code?


  17. Hi Bryan,

    I tried this command in my project root directory,

    titanium-code-processor analyze -o iphone –all-plugins

    │ Description
    │ File
    │ Line │
    │ An exception was thrown but not caught: Cannot dereference require of undefine
    d │ C:SaamyWorkspaceMobileAppCodeBaseProjectNameResourcesapp.js │ 1 │

    it always show error in app.js which is inside Resource folder. But i’m using Alloy , so all my codes are in app folder not in Resource folder. Titanium itself creating this folder when Compiling the code.

    But we want to see error in code we wrote. So that we can correct our errors instead of showing error in Titanium Generated code (Resource Folder).

    Any help you can give on this will be greatly appreciated.


  18. Hi Saamy,

    The command should be titanium-code-processor analyze -o iphone -–all-plugins

    with two dashes before all-plugins, not one. That would explain the behavior you are seeing (require is one of the plugins).

  19. Thanks for quick response Bryan,

    Tried this command

    C:SaamyWorkspaceProjectPathProjectName>titanium-code-processor analyze -o android –all-plugins -d C:SaamyWorkspaceProjectPathProjectName

    Got below Output.

    Could you please correct me if i’m missed something in this command?

    Titanium Code Processor v0.3.2
    Copyright (c) 2012-2013, Appcelerator, Inc. All Rights Reserved.
    info: Analyzing project at “C:SaamyWorkspaceProjectPathProjectName”
    info: Code processing finished successfully in 22745 ms.

    │ Description
    │ File
    │ Line │
    │ An exception was thrown but not caught: Cannot dereference serverurl of undefi
    ned │ C:SaamyWorkspaceProjectPathProjectNameResourcesalloycontrollersindex.js │ 3 │
    │ An exception was thrown but not caught: Cannot dereference property “toUpperCa
    se” of undefined │ C:SaamyWorkspaceProjectPathProjectNameResourcesalloy.js │ 2 │

    │ Description │ File

    │ Line │
    │ A value that could not be evaluated was passed to require │ C:SaamyWorkspace
    index.js │ 124 │
    │ A value that could not be evaluated was passed to require │ C:SaamyWorkspace
    │ 60 │


    Thanks in Advance,

  20. Problem is still it showing error inside resource folder which is generated by Titanium.


  21. Hi Sammy,

    Couple of notes:

    1) Alloy is not supported as a first class citizen yet in the code processor, although proper support is coming in 3.1.1. Suffice to say, until then your mileage may vary.

    2) The errors could very well be actual errors in your code. The code processor tries to evaluated as much code as it realistically can, meaning it may have evaluated some part of your code that isn’t normally run. Or it could just be a bug in the code processor 🙂

    3) The warnings are the more interesting part. The code processor tries to figure out as much as it can about your code, but sometimes it just simply can’t figure something out. Consider this chunk of code:

    var x =;

    In this case, the code processor has no way of knowing what the date will be since it changes every time the app is run, so it simply says “I don’t know what x is going to be, so let’s just leave it as unknown.” If one of these values gets passed in to require(), then, by extension, it doesn’t know what to require. This is what is happening with those warnings. If possible, refactoring your require calls to be more explicit will allow the code processor to figure out what those values are and to continue on processing.

Comments are closed.