Akhilesh Kumar Akhilesh Kumar - 15 days ago 11
Node.js Question

I'm using let keyword in for loop gives SyntaxError: Unexpected identifier

I'm using let keyword in for loop as following

for(let methd1 in servUrl){
let methd=methd1;
for(let pth1 in servUrl[methd]) {
let pth=pth1;
app[methd](pth, servUrl[methd][pth]);
}
}


which gives me following error

for(let methd1 in servUrl){
^^^^^^
SyntaxError: Unexpected identifier

Answer

Can you please make sure, if you are using the supported version of node.

To know the node version you may run the following command:

$ node --version
v4.2.1

Node older versions do not support the let syntax or EcmaScript6 all features.

I also had experienced the similar issue and upgrading my node to latest solved my issue.

To upgrade node you can run the followings:

$ sudo npm cache clean -f
$ sudo npm install -g n
$ sudo n stable