29

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?

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
  • which version of node to you have? – derp May 08 '17 at 23:29
  • 2
    Node v7 apparently: http://node.green/#ES2017-features-Object-static-methods-Object-entries …or any of the widely available polyfills. And no, it's not part of ES7. – Bergi May 08 '17 at 23:29
  • 2
    Object.entries is part of ECMAScript 2018, which I guess is ed 9 (the current version is [*ECMA-262 ed 7*](http://ecma-international.org/ecma-262/7.0/index.html) 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*](https://tc39.github.io/ecma262/). – RobG May 08 '17 at 23:36
  • 1
    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). – Felix Kling May 09 '17 at 00:55
  • @RobG: According to https://github.com/tc39/proposals/blob/master/finished-proposals.md it's supposed to be included in this year's release. – Felix Kling May 09 '17 at 00:58
  • @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. – RobG May 09 '17 at 02:31

7 Answers7

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;
   };
peja
  • 866
  • 10
  • 19
  • the problem with this is that entries sort the arrays based on the insertion order, whereas keys has a different way of sorting. – laurapons Jul 10 '18 at 12:22
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
derp
  • 2,300
  • 13
  • 20
  • 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? – RobG May 09 '17 at 02:34
  • 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: https://nodejs.org/en/docs/es6/ – derp May 09 '17 at 04:08
  • 3
    Perhaps they should consider making the flag something like `--latest` or similar. One less character to type as well. ;-) – RobG May 09 '17 at 04:49
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+

Snekse
  • 15,474
  • 10
  • 62
  • 77
  • 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). – Alan P. Dec 23 '19 at 22:42
  • yes, older versions of node can certainly be the issue to this function not being recognized – Erich Meissner Jun 21 '21 at 18:47
6

you could use babel-polyfill for quick solution

npm install babel-polyfill

import 'babel-polyfill';
deepak prakash
  • 713
  • 9
  • 6
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';
lwz7512
  • 81
  • 1
  • 2
1

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

Chris
  • 4,212
  • 5
  • 37
  • 52
1

Instead of

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

myObj.entries()

do

Object.entries(myObj)
Boris Verkhovskiy
  • 14,854
  • 11
  • 100
  • 103