satyajit
satyajit

Reputation: 1570

How to use node modules with in UIAutomation

According to apple's documentation I can import one JS file into another with an import statement. And yes I am able to use JS functions and recursively call other JS functions.

But can I include node modules into my automation. Node/npm module seems to have a lot of tools that makes life easier and avoid code duplication.

And actually I was able to use one node module called moment.js through the following call in my code

#import "../node_modules/moment/moment.js"

But I am not have the same luck with other npm modules. I tried couple Faker.js, Charlatan.js and I getting the following error in Faker.js

Script threw an uncaught JavaScript error: Can't find variable: window on line 618 of Faker.js

Looking at *.js files it looks like it has something to do with the way these modules are packaged. My JS knowledge isn't getting me anywhere.

The last few lines of moment js file

// CommonJS module is defined
if (hasModule) {
    module.exports = moment;
}
/*global ender:false */
if (typeof ender === 'undefined') {
    // here, `this` means `window` in the browser, or `global` on the server
    // add `moment` as a global object via a string identifier,
    // for Closure Compiler "advanced" mode
    this['moment'] = moment;
}
/*global define:false */
if (typeof define === "function" && define.amd) {
    define("moment", [], function () {
        return moment;
    });
}

Last few lines of Faker js file

if (typeof define == 'function'){
   define(function(){
        return Faker;
   });
}
else if(typeof module !== 'undefined' && module.exports) {
    module.exports = Faker;
}
else {
    window.Faker = Faker;
}

I am perfectly able to play with these modules in node console, so nothing wrong with the modules, it just how to include/require them in my JS files.

Upvotes: 3

Views: 591

Answers (1)

satyajit
satyajit

Reputation: 1570

Had to do two things for Faker to work for me

  1. remove 'use strict'
  2. Check if window is undefined
  3. Add this statement

    this['Faker'] = Faker;

Upvotes: 2

Related Questions