Anshuman Dhamoon Anshuman Dhamoon - 11 months ago 80
Node.js Question

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) {
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

Answer Source

Could be the node version you are using. You'll need to use the flag --harmony_destructuring when using a lower version.

Taken from:

# 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>