SyntaxError:无法在我的js文件的模块外部使用import语句

我正在学习ES6,并且正在学习免费的在线课程。在课程中,我们涵盖了Promises,所以我有:

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();

运行代码(使用Visual Studio Code)时,我得到:

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

我想念什么吗?我在Google上调查了错误,因此安装了交叉提取功能,并且还运行了npm create-react-app es6course,因为我认为我需要运行React才能使它正常工作,并且仍然收到相同的错误消息。

davidscofield 回答:SyntaxError:无法在我的js文件的模块外部使用import语句

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3170146.html

大家都在问