How do I include a .js file in Titanium studio(in index.js)?

480 views Asked by At

I wan't to create a .js file which has helper functions. These functions should be called in the

index.js

I get the following error:

[ERROR] :  Script Error Couldn't find module: ./helper/WBMHelperFunctions.js for architecture: arm64

Here is the code: (index.js)

const TAB_NUMBER = 5;
const TAB_NAMES = ["Start","Sales Partner","Products","About us","Contact"];

var helperFunctionsModule = require('./helper/WBMHelperFunctions.js');

var tabBarController = helperFunctionsModule.createTabBarControllerWithNumberOfTabs(TAB_NUMBER,TAB_NAMES);
var mainWindow = Titanium.UI.createWindow();



mainWindow.add(tabBarController);

(WBMHelperFunctions.js)

function createTabBarControllerWithNumberOfTabs(tabsNumber,tabNamesArray)
{
    var tabBarController = Titanium.UI.createTabGroup();

    for(i = 0 ; i < tabsNumber ; i++)
    {
        //create N windows for N tabs
        var win = Titanium.UI.createWindow({
            title:tabNamesArray[i]
        });

        var tab = Titanium.UI.createTab({
            title:tabNamesArray[i],
            window:win
        });
        tabBarController.add(tab);
    }

    return tabBarController;

}
2

There are 2 answers

0
Emil On

This was asked two months ago, so hopefully you have your answer by now, but for anyone who comes across this page:

Assuming that the file is in the path you specified, then

var helperFunctionsModule = require('./helper/WBMHelperFunctions.js');

is incorrect because of the ".js".

It should be:

var helperFunctionsModule = require('./helper/WBMHelperFunctions');
0
Rajendrasinh Parmar On

You can have your helper files inside lib folder in your App.

if you are using Alloy for titanium project than you can create require helper file in lib folder under app folder in your project.

to access or include any file in your project you can use following code.in this example we are using testHelper.js file to include in our index.js file.

var helper = requier("testHelper");

Note: do not use .js extension while including file placed in lib folder.