Kumar KS
Kumar KS

Reputation: 521

Js giving Unexpected identifier

When i try to execute the js file from cmd line, i am getting this error. I am not sure whether we can give the keys like that.

const App42 = require('./js/App42-all-1.6.min');
const mod = require('./js/buddy');

module.exports = {

var API_KEY = '014883a05a4902889c860272b3c4*******568072cf82cfc31a42c165f0f8cc6';
var SEC_KEY = 'b3b2df57b86a7fdabe66a96a*******7e04fbd95c6ddef942a3c844878eafbf05';


App42.initialize(API_KEY,SEC_KEY); 

hello: function() {
  return "Hello";
}
}

The error i am getting is :

LB.js:6
    var APIKEY = '014883a05a4902889c860272b3c2170ae145*****2cf82cfc31a42c165f0f8cc6';
        ^^^^^^

SyntaxError: Unexpected identifier
at createScript (vm.js:80:10)
at Object.runInThisContext (vm.js:139:10)
at Module._compile (module.js:617:28)
at Object.Module._extensions..js (module.js:664:10)
at Module.load (module.js:566:32)
at tryModuleLoad (module.js:506:12)
at Function.Module._load (module.js:498:3)
at Function.Module.runMain (module.js:694:10)
at startup (bootstrap_node.js:204:16)
at bootstrap_node.js:625:3

Before giving down votes can some one say the solution. This is not how you respond to a new user trying to learn js.

Upvotes: -1

Views: 201

Answers (2)

Marc Verdaguer
Marc Verdaguer

Reputation: 153

The problem here is that you are declaring variables and running functions directly inside an object declaration (module.exports), that's why it does not compile.

You should do something like:

const App42 = require('./js/App42-all-1.6.min');
const mod = require('./js/buddy');

const API_KEY = '014883a05a4902889c860272b3c4*******568072cf82cfc31a42c165f0f8cc6';
const SEC_KEY = 'b3b2df57b86a7fdabe66a96a*******7e04fbd95c6ddef942a3c844878eafbf05';
App42.initialize(API_KEY,SEC_KEY); 

module.exports = {
  hello: function() {
    return "Hello";
  }
}

Upvotes: 0

TommyBs
TommyBs

Reputation: 9648

You're exporting an object, yet you have an invalid object syntax:

module.exports = {
//Shouldn't be using var here as it's just a property of an object
var API_KEY = '014883a05a4902889c860272b3c4*******568072cf82cfc31a42c165f0f8cc6';
var SEC_KEY = 'b3b2df57b86a7fdabe66a96a*******7e04fbd95c6ddef942a3c844878eafbf05';

//Shouldn't be calling a function here as it's also in the object definition
App42.initialize(API_KEY,SEC_KEY); 

hello: function() {
  return "Hello";
}
}

You should just be exporting keys and values for the object. So depending on what you want to actual 'export' you could try

API_KEY:'014883a05a4902889c860272b3c4*******568072cf82cfc31a42c165f0f8cc6',
SEC_KEY:'b3b2df57b86a7fdabe66a96a*******7e04fbd95c6ddef942a3c844878eafbf05',
App42.initialize(API_KEY,SEC_KEY); 
module.exports = {

 API_KEY:API_KEY,
 SEC_KEY:SEC_KEY,
 App42: App42,



hello: function() {
  return "Hello";
}
}

When you then require this file you can access various properties and functions such as

const foo = require('./myfile');
console.log(foo.App42);
console.log(foo.hello());
console.log(foo.API_KEY);

You can read more about objects here

Upvotes: 2

Related Questions