[英]SyntaxError: Cannot use import statement outside of module
我已经查阅了三份文档和其他资源,但我无法修复我的错误。 我刚刚学习了 JavaScript 并且我正在尝试 fetch() 命令但是我无法通过这个错误(标题)
这是我的代码
索引.js
import fetch from 'node-fetch';
fetch('http://example.com/movies.json')
.then(response => response.json())
.then(data => console.log(data));
package.json
{
"name": "Weather-API",
"version": "1.0.0",
"description": "",
"main": "index.js",
"type": "module",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"node-fetch": "^3.0.0"
}
}
正如 Chris G 所提到的,这段代码工作正常。
代码:
fetch('https://jsonplaceholder.typicode.com/posts/1')
.then(response => response.json())
.then(data => console.log(data));
Output:
{
"userId": 1,
"id": 1,
"title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
"body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto"
}
请删除导入行并重试。 如果仍然失败,请检查您的节点版本,我使用 14.17.0 成功尝试。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.