Trying to run ripple-lib with node facing syntax error - node.js

Trying to run the rippleApi with ripple-lib facing issues like syntaxerror .
Am new to Node and ripple-lib . Please guide me to get rid of this. Thanks in advance
node ripple.js
My Code in ripple.js is
const RippleAPI = require('ripple-lib').RippleAPI;
const Ripple = new RippleAPI({
server: 'wss://s1.ripple.com:443'
});
Ripple.on('error', function (errorCode, errorMessage) {
resp.json('{"status":0,"msg":"Unable to withdraw, problem occured. '+errorMessage+'."}');
});
Ripple.on('connected', function () {
});
Ripple.on('disconnected', function (code) {
console.log('disconnected, code:', code);
});
Ripple.connect().then(function () {
return Ripple.getServerInfo();
}).then(function (server_info) {
var rippleAddress = Ripple.generateAddress();
console.log(rippleAddress);process.exit(-1);
}).catch(console.error);
The Output am getting is
/var/www/html/node_modules/ripple-lib/dist/npm/api.js:83
constructor(options = {}) {
^
SyntaxError: Unexpected token =
at exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:374:25)
at Object.Module._extensions..js (module.js:417:10)
at Module.load (module.js:344:32)
at Function.Module._load (module.js:301:12)
at Module.require (module.js:354:17)
at require (internal/module.js:12:17)
at Object.<anonymous> (/var/www/html/node_modules/ripple-lib/dist/npm/index.js:3:13)
at Module._compile (module.js:410:26)
at Object.Module._extensions..js (module.js:417:10)

You need to upgrade nodejs
curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash -
sudo apt-get install -y nodejs

Related

NodeJS mssql error: SyntaxError: Unexpected token '?'

I'm creating a very basic project with Nodejs for accessing SQL Server database. The steps I take are:
I create the folder for the app ang go into it
I execute "npm init -y"
I execute "npm install mssql"
I copy the short example of the documentation (changing the connetion string values) in index.js:
const sql = require('mssql');
async () => {
try {
await sql.connect('Server=localhost,1433;Database=database;User Id=username;Password=password;Encrypt=true');
const result = await sql.query("select * from mytable");
console.dir(result);
} catch (err) {
console.log(err);
}
};
I execute "node index.js"
Then I get the following error:
C:\Users\User\Desktop\project\app\node_modules\tedious\lib\connection.js:1448
const [, major, minor, build] = /^(\d+)\.(\d+)\.(\d+)/.exec(_package.version) ?? ['0.0.0', '0', '0', '0'];
^
SyntaxError: Unexpected token '?'
at wrapSafe (internal/modules/cjs/loader.js:915:16)
at Module._compile (internal/modules/cjs/loader.js:963:27)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
at Module.load (internal/modules/cjs/loader.js:863:32)
at Function.Module._load (internal/modules/cjs/loader.js:708:14)
at Module.require (internal/modules/cjs/loader.js:887:19)
at require (internal/modules/cjs/helpers.js:74:18)
at Object.<anonymous> (C:\Users\User\Desktop\project\app\node_modules\tedious\lib\tedious.js:59:42)
at Module._compile (internal/modules/cjs/loader.js:999:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
What can I do for fixing that and be able to execute the code?

Blinking led light using nodejs

I am performing my first IOT project hello world blinking I'm trying to started with web of things book and having a problem.
The book is this: https://webofthings.org/2016/10/23/node-gpio-and-the-raspberry-pi/.
But When I trying to run the code I get the error.
/home/pi/Desktop/hello_world/node_modules/onoff/onoff.js:9 const HIGH_BUF = Buffer.from('1');
TypeError: this is not a typed array.
at Function.from (native)
at Object.<anonymous>
(/home/pi/Desktop/faizan/node_modules/onoff/onoff.js:9:25)
at Module._compile (module.js:409:26)
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 Module.require (module.js:353:17)
at require (internal/module.js:12:17)
at Object.<anonymous> (/home/pi/Desktop/faizan/app.js:1:75)
at Module._compile (module.js:409:26)
The code I'm running is
var onoff = require('onoff');
var Gpio = onoff.Gpio,
led = new Gpio(4,'out'),
interval;
interval = setInterval(function (){
var value = (led.readSync() + 1) % 2;
led.write(value, function() {
console.log("Changed LED state to: " + value);
});
}, 2000);
process.on('SIGINT', function () {
clearInterval(interval);
led.writeSync(0);
led.unexport();
console.log('Bye, bye!');
process.exit();
});

Azure Chatbot fails with "SyntaxError: Unexpected identifier - cosmosClient.databases.createIfNotExists" while connecting to CosmosDB

I am extremely new to Azure Bot Services and the Azure platform as a whole.
I am trying to create a Chatbot using node.js but I am getting the below error while trying to connect to CosmosDB.
The bot was running fine before I added the below code to connect to CosmosDB.
Any help or guidance on this would be appreciated!
P.S. - I have added the '#azure/cosmos' package and the code runs without any error if I just remove the try-catch segment.
Code for connecting to CosmosDB:
var async=require("async");
var await=require("await");
const CosmosClientInterface = require("#azure/cosmos").CosmosClient;
const databaseId = "ToDoList";
const containerId = "custInfo";
const endpoint = "<Have provided the Endpoint URL here>";
const authKey = "<Have provided the AuthKey here>";
const cosmosClient = new CosmosClientInterface({
endpoint: endpoint,
auth: {
masterKey: authKey
},
consistencyLevel: "Session"
});
async function readDatabase() {
const { body: databaseDefinition } = await cosmosClient.database(databaseId).read();
console.log(`Reading database:\n${databaseDefinition.id}\n`);
}
Error Message:
Sat Jan 12 2019 03:40:08 GMT+0000 (Coordinated Universal Time): Application has thrown an uncaught exception and is terminated:
D:\home\site\wwwroot\app.js:40
async function readDatabase() {
^^^^^^^^
SyntaxError: Unexpected token function
at Object.exports.runInThisContext (vm.js:76:16)
at Module._compile (module.js:542:28)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (D:\Program Files (x86)\iisnode\interceptor.js:459:1)
at Module._compile (module.js:570:32)
Application has thrown an uncaught exception and is terminated:
D:\home\site\wwwroot\app.js:40
async function readDatabase() {
^^^^^^^^
SyntaxError: Unexpected token function
at Object.exports.runInThisContext (vm.js:76:16)
at Module._compile (module.js:542:28)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (D:\Program Files (x86)\iisnode\interceptor.js:459:1)
at Module._compile (module.js:570:32)
You can't await without being in an async function.
Dump all your code into a async function main(){} method, then call main().catch((err) => console.log(err)); or some similar thing to start the promise and handle errors.
You can see a sample of that kind of pattern here in this sample: https://github.com/Azure/azure-cosmos-js/blob/master/samples/ChangeFeed/app.js#L33
--- EDIT 1 ---
Here's your sample rewritten with Promises:
const CosmosClientInterface = require("#azure/cosmos").CosmosClient;
const databaseId = "ToDoList";
const containerId = "custInfo";
const endpoint = "<Have provided the Endpoint URL here>";
const authKey = "<Have provided the AuthKey here>";
const cosmosClient = new CosmosClientInterface({
endpoint: endpoint,
auth: {
masterKey: authKey
},
consistencyLevel: "Session"
});
cosmosClient.database(databaseId).read().then(({body: databaseDefinition}) => {
console.log(`Reading database:\n${databaseDefinition.id}\n`);
}).catch((err) {
console.err("Something went wrong" + err);
});
For your sample above, you don't need to import async/await, they are keywords in JavaScript now.
Here's a blog post that compares and contrasts Async/Await and Promises: https://hackernoon.com/should-i-use-promises-or-async-await-126ab5c98789

Unable to require node-wit

I had been using node-wit v3.3.2
Today, I wanted to update and use the latest version.
But I'm unable to import node-wit. Not sure why.
I simply copied the code given in their documentation.
'use strict'
var MY_TOKEN="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
const {Wit, log} = require('node-wit');
const client = new Wit({
accessToken: MY_TOKEN,
actions: {
send(request, response) {
return new Promise(function(resolve, reject) {
console.log(JSON.stringify(response));
return resolve();
});
},
myAction({sessionId, context, text, entities}) {
console.log(Session ${sessionId} received ${text});
console.log(The current context is ${JSON.stringify(context)});
console.log(Wit extracted ${JSON.stringify(entities)});
return Promise.resolve(context);
}
},
logger: new log.Logger(log.DEBUG) // optional
});
The terminal shows this:
const {Wit, log} = require('node-wit');
^
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:974:3
Could be the node version you are using. You'll need to use the flag --harmony_destructuring when using a lower version.
Taken from: https://github.com/wit-ai/node-wit
# Node.js <= 6.x.x, add the flag --harmony_destructuring
node --harmony_destructuring examples/basic.js <MY_TOKEN>
# Node.js >= v6.x.x
node examples/basic.js <MY_TOKEN>

SyntaxError in plugin 'gulp-mocha' Unexpected token =

I am getting the following error in Travis-ci when using Node 4.3.2:
SyntaxError in plugin 'gulp-mocha' Unexpected token =
Everything works when using Node 6, but not 4.3.2. Here is the stack trace:
[12:05:11] Starting 'build'...
[12:05:11] Finished 'build' after 13 μs
[12:05:11] Starting 'tests'...
SyntaxError in plugin 'gulp-mocha'
Message:
Unexpected token =
Stack:
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 Module.require (module.js:353:17)
at require (internal/module.js:12:17)
This is the task I am running:
gulp.task('tests', ['build'], (cb) => {
if (hasError) {
cb();
return;
}
return gulp.src('build/test/**/*.js', {read: false})
.pipe(mocha())
.on("error", handleError);
});
How can I correct this?
The older version of Node is being used, as this is an AWS Lambda application.

Resources