TypeError: Object.entries is not a function
Asked Answered
B

7

30

Why do I keep getting this error when trying to run my Node.js/Express server?

Is this a part of the newer ES7? What do I need to be able to run an app using these new features?

Bedroll answered 8/5, 2017 at 23:27 Comment(6)
which version of node to you have?Antivenin
Node v7 apparently: node.green/… …or any of the widely available polyfills. And no, it's not part of ES7.Coveney
Object.entries is part of ECMAScript 2018, which I guess is ed 9 (the current version is ECMA-262 ed 7 or ECMAScript 2016, the next will be ECMAScript 2017). However, the edition number seems to have been dropped in the most recent versions, the latest draft is just ECMAScript 2018.Annalee
There is no such thing as "newer ES7". ES7 was released last year. What you are asking about is often called "ES next" (next, upcoming features).Articulation
@RobG: According to github.com/tc39/proposals/blob/master/finished-proposals.md it's supposed to be included in this year's release.Articulation
@FelixKling—I couldn't find a link to ECMAScript 2017, is there one? It's not up on ecma-international.org yet and TC39 has moved on to 2018. It would be nice if there was an official list somewhere that showed when each feature was introduced (it could even be included in the spec). MDN tries with its links to initial and current definition specs but something more authoritative would be nice.Annalee
M
17

On mdn docs, there is a clear tutorial on Object.entries, and it is described what to be done if Object.entries is not supported on part PolyFill in the same page.

To add compatible Object.entries support in older environments that do not natively support it, you can find a demonstrational implementation of Object.entries in the tc39/proposal-object-values-entries (if you don't need any support for IE), a polyfill in the es-shims/Object.entries repositories, or you can use the simple, ready to deploy polyfill listed below.

if (!Object.entries)
   Object.entries = function( obj ){
      var ownProps = Object.keys( obj ),
         i = ownProps.length,
         resArray = new Array(i); // preallocate the Array

      while (i--)
         resArray[i] = [ownProps[i], obj[ownProps[i]]];
      return resArray;
   };
Mirilla answered 26/12, 2017 at 12:8 Comment(1)
the problem with this is that entries sort the arrays based on the insertion order, whereas keys has a different way of sorting.Arctic
A
13

According to http://kangax.github.io/compat-table/es2016plus/ under the Object static methods, it seems you need to enable the harmony flag

So run node like this

node --harmony script.js
Antivenin answered 8/5, 2017 at 23:34 Comment(3)
Wasn't "Harmony" supposed to be ECMAScript 2015 aka ECMA-262 ed 6? Or does it now just refer to the latest (maybe still in draft) features?Annalee
given that Object.entries seems to be a ES2017 feature, the harmony flag now represents features that are almost complete but not considered stable. See here: nodejs.org/en/docs/es6Antivenin
Perhaps they should consider making the flag something like --latest or similar. One less character to type as well. ;-)Annalee
M
8

In case this helps someone else...

Update your version of Node. I was running node 6.x and this issue resolved itself after I updated to node 8.x+

Milano answered 9/6, 2018 at 17:3 Comment(2)
My SublimeLinter was having this error when it ran, and I resolved it by updating node (had 6.2.2) and making sure SublimeLinter's settings pointed to the new version (10.15.1).Banshee
yes, older versions of node can certainly be the issue to this function not being recognizedKerriekerrigan
H
7

you could use babel-polyfill for quick solution

npm install babel-polyfill

import 'babel-polyfill';
Hephaestus answered 11/11, 2017 at 11:8 Comment(1)
This didn't work for me and I followed the docs exactly. You should make sure your answer actually works before posting.Scholarship
M
3

First install react-app-polyfill:

npm install react-app-polyfill

Then import to the top of your index.jsx before import React:

import 'react-app-polyfill/stable';
Malinda answered 20/2, 2020 at 16:37 Comment(0)
A
2

Instead of

const myObj = {a: 1, b: 2}

myObj.entries()

do

Object.entries(myObj)
Aeriell answered 18/7, 2021 at 9:47 Comment(0)
J
1

In my case it resolved itself when I switched from node 11.15.0 to lts/erbium / 12.18.3.

Jamie answered 11/8, 2020 at 14:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.