How do I use jquery within the serverless framework? - javascript

I would like to make use of the jquery deferred method in my serverless lambda project. However after requiring both jquery & jquery-ui as dependencies, the following error occurs when I attempt to make use of the jquery library? I'm new to serverless and lambda so please see the handler.js function below.
TypeError: $.each is not a function
at /vagrant/project/node_modules/jquery-ui/ui/widget.js:690:3
at widgetUuid (/vagrant/project/node_modules/jquery-ui/ui/widget.js:24:3)
at Object.<anonymous> (/vagrant/project/node_modules/jquery-ui/ui/widget.js:26:2)
at Module._compile (module.js:569:30)
at Object.Module._extensions..js (module.js:580:10)
at Module.load (module.js:503:32)
at tryModuleLoad (module.js:466:12)
at Function.Module._load (module.js:458:3)
at Module.require (module.js:513:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (/vagrant/project/handler.js:4:19)
at Module._compile (module.js:569:30)
at Object.Module._extensions..js (module.js:580:10)
at Module.load (module.js:503:32)
at tryModuleLoad (module.js:466:12)
at Function.Module._load (module.js:458:3)
at Module.require (module.js:513:17)
at require (internal/module.js:11:18)
at AwsInvokeLocal.invokeLocalNodeJs (/usr/local/lib/node_modules/serverless/lib/plugins/aws/invokeLocal/index.js:156:33)
at AwsInvokeLocal.invokeLocal (/usr/local/lib/node_modules/serverless/lib/plugins/aws/invokeLocal/index.js:114:19)
at AwsInvokeLocal.tryCatcher (/usr/local/lib/node_modules/serverless/node_modules/bluebird/js/release/util.js:16:23)
at Promise._settlePromiseFromHandler (/usr/local/lib/node_modules/serverless/node_modules/bluebird/js/release/promise.js:512:31)
at Promise._settlePromise (/usr/local/lib/node_modules/serverless/node_modules/bluebird/js/release/promise.js:569:18)
at Promise._settlePromise0 (/usr/local/lib/node_modules/serverless/node_modules/bluebird/js/release/promise.js:614:10)
at Promise._settlePromises (/usr/local/lib/node_modules/serverless/node_modules/bluebird/js/release/promise.js:693:18)
at Async._drainQueue (/usr/local/lib/node_modules/serverless/node_modules/bluebird/js/release/async.js:133:16)
at Async._drainQueues (/usr/local/lib/node_modules/serverless/node_modules/bluebird/js/release/async.js:143:10)
at Immediate.Async.drainQueues (/usr/local/lib/node_modules/serverless/node_modules/bluebird/js/release/async.js:17:14)
at runCallback (timers.js:800:20)
at tryOnImmediate (timers.js:762:5)
at processImmediate [as _immediateCallback] (timers.js:733:5)
handler.js
'use strict';
global.jQuery = require('jquery');
global.jQueryUI = require('jquery-ui');
var Model = require('./resources/model');
module.exports.project = function(event, context, callback) {
Model.init();
};
model.js
var JsonFile = require('jsonfile');
var $ = global.jQueryUI;
module.exports = {
init : function() {
var self = this;
self.fetch_file().done(function(file){
console.log(file);
});
},
fetch_file : function(){
var deferred = $.Deferred();
JsonFile.readFile('path to file', function (err, file) {
deferred.resolve(file);
});
return $.when(deferred).promise();
}
};
package.json
{
"private": true,
"dependencies": {
"avro-js": "^1.8.2",
"aws-sdk": "^2.88.0",
"jquery": "^3.2.1",
"jquery-ui": "^1.12.1",
"jsonfile": "^3.0.1"
},
"name": "project",
"version": "0.1.0"
}
After requiring Jquery instead of JqueryUI I get this exception:
TypeError: $.Deferred is not a function
at Object.fetch_schema (/vagrant/project/resources/model.js:17:26)
at Object.init (/vagrant/project/resources/model.js:11:14)
at module.exports.pixel_event_lambda_producer (/vagrant/project/handler.js:9:10)
at AwsInvokeLocal.invokeLocalNodeJs (/usr/local/lib/node_modules/serverless/lib/plugins/aws/invokeLocal/index.js:229:12)
at AwsInvokeLocal.invokeLocal (/usr/local/lib/node_modules/serverless/lib/plugins/aws/invokeLocal/index.js:114:19)
From previous event:
at Object.invoke:local:invoke [as hook] (/usr/local/lib/node_modules/serverless/lib/plugins/aws/invokeLocal/index.js:24:10)
at BbPromise.reduce (/usr/local/lib/node_modules/serverless/lib/classes/PluginManager.js:218:55)
From previous event:
at PluginManager.invoke (/usr/local/lib/node_modules/serverless/lib/classes/PluginManager.js:218:22)
at PluginManager.run (/usr/local/lib/node_modules/serverless/lib/classes/PluginManager.js:237:17)
at variables.populateService.then (/usr/local/lib/node_modules/serverless/lib/Serverless.js:99:33)
at runCallback (timers.js:800:20)
at tryOnImmediate (timers.js:762:5)
at processImmediate [as _immediateCallback] (timers.js:733:5)
From previous event:
at Serverless.run (/usr/local/lib/node_modules/serverless/lib/Serverless.js:86:74)
at serverless.init.then (/usr/local/lib/node_modules/serverless/bin/serverless:39:50)
at <anonymous>

Issue resolved when I specifically required the jquery-deferred module which can be found here: https://www.npmjs.com/package/jquery-deferred

You have confused jQuery with jQuery UI. In your model.js, replace:
var $ = global.jQueryUI;
With:
var $ = global.jQuery;
Deferred is a jQuery method, and not a jQuery UI feature.

Related

'classifierType is not a constructor'

I'm using the limdu library found here (https://www.npmjs.com/package/limdu#binary-classification). I'm trying to combine the 'Multi-Label Classification' section with the 'Input Normalization' section to make the outputs all be lowercase, but I keep getting the following error:
/Users/nickbrinsmade/Desktop/limdutest/node_modules/limdu/classifiers/EnhancedClassifier.js:43
this.classifier = new opts.classifierType();
^
TypeError: opts.classifierType is not a constructor
at new EnhancedClassifier (/Users/myusr/Desktop/limdutest/node_modules/limdu/classifiers/EnhancedClassifier.js:43:20)
at Object.<anonymous> (/Users/myusr/Desktop/limdutest/index.js:9:20)
at Module._compile (internal/modules/cjs/loader.js:1072:14)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1101:10)
at Module.load (internal/modules/cjs/loader.js:937:32)
at Function.Module._load (internal/modules/cjs/loader.js:778:12)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:76:12)
at internal/main/run_main_module.js:17:47
My code Is seen below:
var limdu = require('limdu');
var MyWinnow = limdu.classifiers.Winnow.bind(0, {retrain_count: 10});
var textClassifier = new limdu.classifiers.multilabel.BinaryRelevance({
binaryClassifierType: MyWinnow
});
intentClassifier = new limdu.classifiers.EnhancedClassifier({
classifierType: textClassifier, // same as in previous example
normalizer: limdu.features.LowerCaseNormalizer
}); //this declaration throws the error.
intentClassifier.trainBatch([
{input: {I:1,want:1,an:1,apple:1}, output: "APPLE"},
{input: {I:1,want:1,a:1,banana:1}, output: "BANANA"},
{input: {I:1,want:1,chips:1}, output: "CHIPS"}
]);
console.log(intentClassifier.classify({I:1,want:1,an:1,apple:1,and:1,a:1,banana:1}));

mocha testing Angular (typescript) app that uses OpenLayers 6 fails with 'turf mocha "syntaxError: Unexpected token {"'

I solved this during the writing of the question and have provided my answer below since it was a bit tricky to work out. I am happy to hear any better or alternative answers.
I have an Angular OpenLayers 6 geomapping app. Being Angular I use Typescript and it transpiles and runs fine. And also being Angular it uses ng test to do the testing. All tests run fine.
However I use mocha + chai for testing in the IDE (IntelliJ) since I don't require UI testing for the mathematical work I'm currently performing (ng test runs the UI tests if and when I need that. But in the IDE I select the tests to run). Testing this way worked fine until I added a new test that creates a new instance of a class that imports GeoJSON:
import GeoJSON from 'ol/format/GeoJSON';
That test fails (in mocha) with:
/home/smx9b6/dev/ng-eow/node_modules/ol/format/GeoJSON.js:17
import { assert } from '../asserts.js';
^
SyntaxError: Unexpected token {
Looking at the GeoJSON.js file it seems to have UMD module format (i think this is UMD):
/**
* #module ol/format/GeoJSON
*/
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
import { assert } from '../asserts.js';
import Feature from '../Feature.js';
var GeoJSON = /** #class */ (function (_super) {
__extends(GeoJSON, _super);
/**
* #param {Options=} opt_options Options.
*/
function GeoJSON(opt_options) {
...
}
GeoJSON.prototype.writeGeometryObject = function (geometry, opt_options) {
return writeGeometry(geometry, this.adaptOptions(opt_options));
};
return GeoJSON;
}(JSONFeature));
And others, such as turf.js use the ES6 module format. eg. line-to-polygon:
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var bbox_1 = require("#turf/bbox");
var invariant_1 = require("#turf/invariant");
var helpers_1 = require("#turf/helpers");
...
function lineToPolygon(lines, options) {
if (options === void 0) { options = {}; }
...
}
...
exports.default = lineToPolygon;
Mocha can't deal with this but Angular can - I don't know why. I run mocha with (as reported by IntelliJ when running the test - full paths removed):
node node_modules/mocha/bin/_mocha --require ts-node/register --ui bdd --reporter mochaIntellijReporter.js
src/app/geometry-ops.spec.ts --grep "^geometry-ops centroid "
I have commonjs set as the module type:
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "./out-tsc/app",
"types": [],
"resolveJsonModule": true,
"module": "commonjs",
"esModuleInterop": true,
"allowSyntheticDefaultImports": true
},
...
}
The error in full is:
/home/smx9b6/dev/ng-eow/node_modules/ol/format/GeoJSON.js:17
import { assert } from '../asserts.js';
^
SyntaxError: Unexpected token {
at Module._compile (internal/modules/cjs/loader.js:723:23)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Module.require (internal/modules/cjs/loader.js:692:17)
at require (internal/modules/cjs/helpers.js:25:18)
at Object.<anonymous> (/home/smx9b6/dev/ng-eow/src/app/layers-geometries.ts:4:1)
at Module._compile (internal/modules/cjs/loader.js:778:30)
at Module.m._compile (/home/smx9b6/dev/ng-eow/node_modules/ts-node/src/index.ts:439:23)
at Module._extensions..js (internal/modules/cjs/loader.js:789:10)
at Object.require.extensions.(anonymous function) [as .ts] (/home/smx9b6/dev/ng-eow/node_modules/ts-node/src/index.ts:442:12)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Module.require (internal/modules/cjs/loader.js:692:17)
at require (internal/modules/cjs/helpers.js:25:18)
at Object.<anonymous> (/home/smx9b6/dev/ng-eow/src/app/geometry-ops.spec.ts:13:1)
at Module._compile (internal/modules/cjs/loader.js:778:30)
at Module.m._compile (/home/smx9b6/dev/ng-eow/node_modules/ts-node/src/index.ts:439:23)
at Module._extensions..js (internal/modules/cjs/loader.js:789:10)
at Object.require.extensions.(anonymous function) [as .ts] (/home/smx9b6/dev/ng-eow/node_modules/ts-node/src/index.ts:442:12)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Module.require (internal/modules/cjs/loader.js:692:17)
at require (internal/modules/cjs/helpers.js:25:18)
at /home/smx9b6/dev/ng-eow/node_modules/mocha/lib/mocha.js:334:36
at Array.forEach (<anonymous>)
at Mocha.loadFiles (/home/smx9b6/dev/ng-eow/node_modules/mocha/lib/mocha.js:331:14)
at Mocha.run (/home/smx9b6/dev/ng-eow/node_modules/mocha/lib/mocha.js:809:10)
at Object.exports.singleRun (/home/smx9b6/dev/ng-eow/node_modules/mocha/lib/cli/run-helpers.js:108:16)
at exports.runMocha (/home/smx9b6/dev/ng-eow/node_modules/mocha/lib/cli/run-helpers.js:142:13)
at Object.exports.handler.argv [as handler] (/home/smx9b6/dev/ng-eow/node_modules/mocha/lib/cli/run.js:292:3)
at Object.runCommand (/home/smx9b6/dev/ng-eow/node_modules/mocha/node_modules/yargs/lib/command.js:242:26)
at Object.parseArgs [as _parseArgs] (/home/smx9b6/dev/ng-eow/node_modules/mocha/node_modules/yargs/yargs.js:1087:28)
at Object.parse (/home/smx9b6/dev/ng-eow/node_modules/mocha/node_modules/yargs/yargs.js:566:25)
at Object.exports.main (/home/smx9b6/dev/ng-eow/node_modules/mocha/lib/cli/cli.js:68:6)
at Object.<anonymous> (/home/smx9b6/dev/ng-eow/node_modules/mocha/bin/_mocha:10:23)
at Module._compile (internal/modules/cjs/loader.js:778:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:831:12)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)
I worked out and provide an answer below. However I would still like to hear any feedback on this. Such as should OpenLayers change their module format? (I'm still getting my head around all the different module formats).
The answer is to add --require esm --require jsdom-global/register to the node / mocha call.
The esm is to specify ES6 as the module format and jsdom-global is to define the DOM in non-browser environments (specifically to define document).
The full command is:
node node_modules/mocha/bin/_mocha --ui bdd \
--require ts-node/register \
--require esm \
--require jsdom-global/register \
--reporter landing \
src/app/geometry-ops.spec.ts --grep "^geometry-ops centroid "
(I added spaces in the command since it irks me when I have to scroll right to see such things).
I am happy to hear any better or alternative answers.

NFC Error in nodejs

I was about to make a program that reads NFC cards via NFC reader.
when I execute my program i get the following errros...
/home/dotmark/Desktop/nfc2/node_modules/bindings/bindings.js:83
throw e
^
Error: libnfc.so.5: cannot open shared object file: No such file or directory
at Object.Module._extensions..node (module.js:598:18)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at Module.require (module.js:498:17)
at require (internal/module.js:20:19)
at bindings (/home/dotmark/Desktop/nfc2/node_modules/bindings/bindings.js:76:44)
at Object.<anonymous> (/home/dotmark/Desktop/nfc2/node_modules/nfc/index.js:1:95)
at Module._compile (module.js:571:32)
at Object.Module._extensions..js (module.js:580:10)
My script for loading nfc libraries is as follows...
var nfc = require('nfc').nfc
, util = require('util')
;
console.log('nfc.version(): ' + util.inspect(nfc.version(), { depth: null }));
// { name: 'libfnc', version: '1.7.0' }
console.log('nfc.scan(): ' + util.inspect(nfc.scan(), { depth: null }));
Any help would be really great..

function rest parameters throw error

running:
$> node restparamstest.js
where:
restparamstest.js
var addTestNotification = function(x, ...theArgs) {
theArgs.forEach(function (post) {
console.log(post);
});
};
addTestNotification(1, 2, 4);
throws:
(function (exports, require, module, __filename, __dirname) { var addTestNotification = function(x, ...theArgs) {
^^^
SyntaxError: Unexpected token ...
at exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:373:25)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Function.Module.runMain (module.js:441:10)
at startup (node.js:139:18)
at node.js:968:3
on node version:
console.log(process.versions);
{ http_parser: '2.5.2',
node: '4.4.7',
v8: '4.5.103.36',
any ideas? Thanks!
Rest parameters are fully supported only since node 6.31. If you want to use them with earlier versions of node, you should use the --harmony flag.
You can see ES2015 support by node version here.
Give it a try like this
node --harmony restparamstest.js

JavaScript Exporting a class definition [duplicate]

This question already has answers here:
Destructuring in Node.JS
(3 answers)
Closed 7 years ago.
Can I export class definition in JavaScript? For example,
in file "HelloWorld.js":
'use strict';
class HelloWorld {
constructor(msg = 'Hello World~') {
this.message = msg;
}
sayHi() {
console.log(this.message);
}
}
module.exports = HelloWorld;
Then in "index.js"
'use strict';
var HelloWorld = require('HelloWorld');
var myObj = new HelloWorld;
myObj.sayHi();
if I do "node index.js", then I got the error below:
constructor(msg = 'Hello World~') {
^
SyntaxError: Unexpected token =
at exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:387:25)
at Object.Module._extensions..js (module.js:422:10)
at Module.load (module.js:357:32)
at Function.Module._load (module.js:314:12)
at Module.require (module.js:367:17)
at require (internal/module.js:16:19)
at Object.<anonymous> (/data/users/soltiho/fbsource/fbcode/video_templates/test_env/index.js:3:18)
at Module._compile (module.js:413:34)
at Object.Module._extensions..js (module.js:422:10)
my node is v5.5.0
Node doesn't support destructuring (yet), and using destructuring would be wrong anyways. There is nothing special about importing/exporting a class. Import it like any other module:
var myValue = require('myVModule');

Categories