How do I make generator functions work with Hapi JS - node.js

I installed various node.js versions with nvm and tried -harmony flag to make generator functions work with yield keyword but I'm getting all kinds of errors when the server starts. One of them is below:
/home/ubuntu/workspace/node_modules/hapi/node_modules/joi/lib/object.js:310
throw castErr;
^
TypeError: Cannot read property '_items' of undefined
at /home/ubuntu/workspace/node_modules/hapi/node_modules/topo/lib/index.js:39:22
at Array.forEach (native)
at internals.Topo.add (/home/ubuntu/workspace/node_modules/hapi/node_modules/topo/lib/index.js:36:24)
at internals.Object.keys (/home/ubuntu/workspace/node_modules/hapi/node_modules/joi/lib/object.js:301:18)
at internals.root.root.object (/home/ubuntu/workspace/node_modules/hapi/node_modules/joi/lib/index.js:71:72)
at Object.<anonymous> (/home/ubuntu/workspace/node_modules/hapi/node_modules/catbox/lib/policy.js:255:24)
at Module._compile (module.js:460:26)
at Object.Module._extensions..js (module.js:478:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
Is there something special I need to do to use the yield keyword?
UPDATE: I updated nodejs to 5.4.1 and the errors are gone. But I can't use the yield function.
Here's a code:
var nodes = yield Db.node.find({ type: 'root' });
return reply.success(nodes);
And here's the error I get:
var nodes = yield Db.node.find({ type: 'root' });
^^
SyntaxError: Unexpected identifier

You'll need a plugin for hapi that allows you to run a generator as handler. Something like https://github.com/ide/hapi-async-handler

Related

NodeJS SyntaxError: Unexpected token '.'

Beginner to Node JS and NPM. The following code throws a Syntax error and can't figure out the issue.
Node version: v12.18.3
NPM version: 7.15.1
Error
LayerObj.options?.["displayName"] != undefined
^
SyntaxError: Unexpected token '.'
at wrapSafe (internal/modules/cjs/loader.js:1053:16)
at Module._compile (internal/modules/cjs/loader.js:1101:27)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1157:10)
at Module.load (internal/modules/cjs/loader.js:985:32)
at Function.Module._load (internal/modules/cjs/loader.js:878:14)
at Module.require (internal/modules/cjs/loader.js:1025:19)
at require (internal/modules/cjs/helpers.js:72:18)
at Object.<anonymous> (/Applications/MAMP/htdocs/nft-generators/hashlips_art_engine-main/index.js:2:39)
at Module._compile (internal/modules/cjs/loader.js:1137:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1157:10)
Code
const layersSetup = (layersOrder) => {
const layers = layersOrder.map((layerObj, index) => ({
id: index,
elements: getElements(`${layersDir}/${layerObj.name}/`),
name:
layerObj.options?.["displayName"] != undefined
? layerObj.options?.["displayName"]
: layerObj.name,
blend:
layerObj.options?.["blend"] != undefined
? layerObj.options?.["blend"]
: "source-over",
opacity:
layerObj.options?.["opacity"] != undefined
? layerObj.options?.["opacity"]
: 1,
bypassDNA:
layerObj.options?.["bypassDNA"] !== undefined
? layerObj.options?.["bypassDNA"]
: false,
}));
return layers;
};
Optional chaining (?.) was added in Node.js 14 and is not directly available in Node 12.
The ?. operator is like the . chaining operator, except that instead of causing an error if a reference is nullish (null or undefined), the expression short-circuits with a return value of undefined. When used with function calls, it returns undefined if the given function does not exist.
Use a more recent version of Node or use Node 12 compatible code.
New JS features in Node.js v14.0 discusses some new language constructs in Node 14.

TypeError [ERR_INVALID_ARG_TYPE]: The first argument must be of type string or an instance of Buffer, ArrayBuffer, or Array or an Array-like Object

For
const systemRegex = /^system\./,
endOfLine = require('os').EOL,
EJSON = require('mongodb-extjson');
I got error at the line EJSON = require('mongodb-extjson'), details is as follows:
TypeError [ERR_INVALID_ARG_TYPE]: The first argument must be of type string or an instance of Buffer, ArrayBuffer, or Array or an Array-like Object. Received function hidden
at Function.from (buffer.js:331:9)
at fnv1a32 (/hdd/all_nlus/nlu_platform_bundle/backend/node_modules/mongodb-extjson/node_modules/bson/lib/bson/fnv1a.js:21:25)
at fnv1a24 (/hdd/all_nlus/nlu_platform_bundle/backend/node_modules/mongodb-extjson/node_modules/bson/lib/bson/fnv1a.js:39:18)
at Object.<anonymous> (/hdd/all_nlus/nlu_platform_bundle/backend/node_modules/mongodb-extjson/node_modules/bson/lib/bson/objectid.js:14:20)
at Module._compile (internal/modules/cjs/loader.js:1138:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1158:10)
at Module.load (internal/modules/cjs/loader.js:986:32)
at Function.Module._load (internal/modules/cjs/loader.js:879:14)
at Module.require (internal/modules/cjs/loader.js:1026:19)
at require (internal/modules/cjs/helpers.js:72:18)
at Object.<anonymous> (/hdd/all_nlus/nlu_platform_bundle/backend/node_modules/mongodb-extjson/node_modules/bson/lib/bson/bson.js:7:14)
at Module._compile (internal/modules/cjs/loader.js:1138:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1158:10)
at Module.load (internal/modules/cjs/loader.js:986:32)
at Function.Module._load (internal/modules/cjs/loader.js:879:14)
at Module.require (internal/modules/cjs/loader.js:1026:19) {
code: 'ERR_INVALID_ARG_TYPE'
}
How to resolve it?
It is because of the Node.js version.
Node.js v10.15.1: Works!
Node.js v12.8.1: Error loading the module
I am not sure about the cause, but I would guess that it is because Node.js' packaging mechanism has undergone a change. And mongodb-extjson has not been modified to adhere to the new mechanism. As mentioned in their GitHub repository, it is unlikely to be fixed as it is no longer being maintained.
You have the following choices:
Downgrade to v10 of Node.js, OR
Use the recommended bson module instead:
$ npm install bson
const { EJSON } = require('bson');
const data = '{ "someId": { "$oid": "5ec7cb151a1878fbefce4119" } }'
const doc = EJSON.parse(data, { relaxed: false });
console.log(doc.someId._bsontype)
// Should print 'ObjectID'
Note the difference in the EJSON option between the two modules. Whereas in mongodb-extjson the default is strict, in bson the default is relaxed.

How to use System.import() correctly?

I use the jspm in my project.
But I need the server side nodejs file to execute some instruction.
For example, I need to use the lodash and found the guide in the https://github.com/systemjs/systemjs
var System = require('jspm').Loader();
System.import('lodash').then(function (_) { console.log(_); });
However, I want to use the lodash globally.
Just like
var _ = System.import('lodash');
var myArr = _.map([1, 2, 3], function(n) { return n * 3; });
It will show
TypeError: _.map is not a function
at Object. (/Users/joyfeel/javascript/jspm-test/index.js:49:16)
at Module._compile (module.js:435:26)
at normalLoader (/usr/local/lib/node_modules/babel/node_modules/babel-core/lib/api/register/node.js:199:5)
at Object.require.extensions.(anonymous function) [as .js] (/usr/local/lib/node_modules/babel/node_modules/babel-core/lib/api/register/node.js:216:7)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:311:12)
at Function.Module.runMain (module.js:467:10)
at Object. (/usr/local/lib/node_modules/babel/lib/_babel-node.js:144:25)
at Module._compile (module.js:435:26)
at Object.Module._extensions..js (module.js:442:10)
Why does the lodash only be used in .then scope?
Could anyone tell me how to figure it out? Suppose we want to System.import other modules and use it?
_ can only be accessed in the scope of then because System.import always returns a Promise.
Therefore you have to wait for the Promise to be resolved before you can access its result.
Anyway i would not recommend you to use lodash globally.
But when you really want to use _ globally you can do something like:
System.import('lodash').then(function(_) {
GLOBAL._ = _;
});
Still you have to make sure that all code that uses GLOBAL._ waits till the Promise from the lodash import is resolved.
But again: i would discourage you doing it that way but recommend that you import lodash in every module that needs it.

proper way of using es6 classes in a nodejs project

I'd like to be able to use the cool es6 classes feature of nodejs 4.1.2
I created the following project:
a.js:
class a {
constructor(test) {
a.test=test;
}
}
index.js:
require('./a.js');
var b = new a(5);
as you can see I create a simple class that it's constructor gets a parameter. and in my include i require that class and create a new object based on that class. pretty simple.. but still i'm getting the following error:
SyntaxError: Block-scoped declarations (let, const, function, class) not yet supported outside strict mode
at exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:413:25)
at Object.Module._extensions..js (module.js:452:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Module.require (module.js:365:17)
at require (module.js:384:17)
at Object.<anonymous> (/Users/ufk/work-projects/bingo/server/bingo-tiny/index.js:1:63)
at Module._compile (module.js:434:26)
at Object.Module._extensions..js (module.js:452:10)
any ideas why ?
Or you can run like this:
node --use_strict index.js
i'm still confused about why 'use strict' is needed, but this is the code that works:
index.js:
"use strict";
var a = require('./a.js');
var b = new a(5);
a.js:
"use strict";
class a {
constructor(test) {
a.test=test;
}
}
module.exports=a;

Node.js with ExpressJS error: Cannot read property 'prototype' of undefined

Running node.js v0.10.2 and express v3.1.1 (latest at this time) and getting this error:
/root/dmr-addresses/node_modules/jquery/lib/node-jquery.js:10
window.XMLHttpRequest.prototype.withCredentials = false;
^
TypeError: Cannot read property 'prototype' of undefined
at create (/root/dmr-addresses/node_modules/jquery/lib/node-jquery.js:10:26)
at /root/dmr-addresses/node_modules/jquery/lib/node-jquery.js:9503:18
at Object.<anonymous> (/root/dmr-addresses/node_modules/jquery/lib/node-jquery.js:9505:2)
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.require (module.js:362:17)
at require (module.js:378:17)
at Object.<anonymous> (/root/dmr-addresses/address/log.js:1:71)
line 1 of log.js is:
var $ = require('jquery');
I've tried running npm install jquery but it has not fixed the problem.
Check this:
Same error here...
I don't know what I'm doing, but I changed the node-jquery.js fourth-fifth row's and it's start working :)
old:
if(window == null ) {
window = require('jsdom').jsdom().createWindow();
new:
if(!window || !window.document) {
window = require('jsdom').createWindow();
window.document = require('jsdom').jsdom();
You don't actually have a prototype object in Node server code, it's all stored in the much nicer __proto__ object and you should be using Object.create/defineProperty.
What exactly are you trying to do? Run an ajax query with Node? If so, you should be using Nodes http.request
An example could be:
require('request').post({
"uri" : "http://example.com/",
"headers" : {
'content-type': 'application/json'
},
"body" : "hello=world"
},
function(e,r,b){
// e = errors, r = response and b = returned body
console.log(b,r.statusCode));
});
Looks like this is an issue with the jsdom module that node-jquery depends on. It appears that this is a known issue, and that it has been fixed, but not published to npm yet.
Check it out: https://github.com/coolaj86/node-jquery/issues/52

Resources