6

I am just learning ES6 and I am following a free online course. In the course we are covering Promises, so I have:

import fetch from 'cross-fetch';

const apiUrl = 'https://fcctop100.herokuapp.com/api/fccusers/top/alltime';

function getTop100Campers(){
    fetch(apiUrl)
    .then((r) => r.json())
    .then((json) => {
        console.log(json[0])
    }).catch((error) => {
        console.log('failed');
    });
}

getTop100Campers();

When I run my code (I am using Visual Studio Code) I get:

C:\Program Files\nodejs\node.exe src\ES6-Course.js
c:\Users\j\Documents\Programming\ES6\es6course\src\ES6-Course.js:1
import fetch from 'cross-fetch';
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at Module._compile (internal/modules/cjs/loader.js:892:18)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:973:10)
    at Module.load (internal/modules/cjs/loader.js:812:32)
    at Function.Module._load (internal/modules/cjs/loader.js:724:14)
    at Function.Module.runMain (internal/modules/cjs/loader.js:1025:10)
    at internal/main/run_main_module.js:17:11

Am I missing something? I looked into the error on Google, so I installed cross-fetch, and I also ran npm create-react-app es6course because I thought I needed React running for this to work and I am still getting the same error message.

missjcohen
  • 59
  • 1
  • 2
  • 5
  • 5
    Changing **import fetch from 'cross-react';** to **const fetch = require('cross-fetch');** fixed my issue :) – missjcohen Nov 03 '19 at 21:20
  • Node v12 added support for ES Modules. But for now, you still need to run node with a flag `node --experimental-modules` to support this syntax. – mjarraya Nov 09 '19 at 11:00

0 Answers0