8
var list = {"you": 100, "me": 75, "foo": 116, "bar": 15};
keysSorted = Object.keys(list).sort(function(a,b){return list[a]-list[b]})
alert(keysSorted);     // bar,me,you,foo

from Sorting JavaScript Object by property value

I'm doing Object sorting. Above source working in Javascript. But not working in Node.js. I have an error like this.

keysSorted = Object.keys(list).sort(function(a,b){return list[a]-list[b]})
                    ^
TypeError: Object.keys is not a function

I don't know why. Please help me.

7
  • What does node -v show? Commented Nov 13, 2016 at 8:05
  • what is node version v 6.7 it's working fine Commented Nov 13, 2016 at 8:08
  • Are you redeclaring Object somewhere? like var Object = 'something else';? Not sure how Node would react to that but seeing TypeError: Object.keys is not a function leaves me just that option... Commented Nov 13, 2016 at 8:25
  • Tested in node 6.2.2 and you can actually do var Object = 'something else'; and erase the Object native value.... can that be your problem? Check your variables... Commented Nov 13, 2016 at 8:27
  • OMG!!!!!!! I found my mistake. I declared var Object = 'requrie file';. Thank u so much guys. You're genius. Commented Nov 13, 2016 at 13:51

1 Answer 1

3

Most likely, you are using an out-dated Node.js version, v0.10 or v0.12. To check the version try running node -v.

This old version doesn't support Object.keys. You can check supported features for every Node.js versions here.

My advice is to update to Node.js version to 6.9.1. This version is current LTS (Long-term support).

Sign up to request clarification or add additional context in comments.

4 Comments

Object.keys() on objects works just fine with node v0.10.
As you say, I update node.js to 6.9.1. But still not workingTT. appearing same error .
I solve it. I declared var Object; Anyway thank you answering! :)
This is incorrect. Object.fit() works fine on Node 0.10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.