提问人:mohsen rostami 提问时间:11/12/2023 最后编辑:halfermohsen rostami 更新时间:11/14/2023 访问量:82
为什么我在 VS Code“Code Runner”中收到错误,但它在浏览器中正常工作?
Why I get an error in VS Code "Code Runner" but it is working properly in the browser?
问:
我有两个 JS 文件,当我在 VS Code 的 Code Runner 中运行主 JS 文件时,出现错误,但它在浏览器中工作正常,为什么会发生这种情况?
我在 HTML 脚本标签中使用了:type="module"
<script src="js/main.js" type="module"></script>
这是导出JS文件:
export const namesOne = {
0: "Code",
1: "Learning",
2: "Coding",
3: "Program",
4: "Coder",
5: "Programming",
6: "Dev",
7: "WebDev",
8: "Web",
9: "Tech"
}
export const namesTwo = {
0: "Life",
1: "MadeEasy",
2: "Programmer",
3: "Simplified",
4: "Development",
5: "World",
6: "Media",
7: "Developer",
8: "Stacker",
9: "WebDev"
}
这是导入JS文件:
import { namesOne, namesTwo } from './names.js';
const initApp = () => {
document.getElementById("submitForm").addEventListener("submit", (event) => {
event.preventDefault();
clearSuggestions();
const namesArray = generateNames();
displayNames(namesArray);
});
}
document.addEventListener("DOMContentLoaded", initApp);
在上面的例子中,我得到 SyntaxError:
SyntaxError:无法在模块外部使用 import 语句 在 internalCompileFunction (node:internal/vm:73:18) 在 wrapSafe (node:internal/modules/cjs/loader:1153:20) 在 Module._compile (node:internal/modules/cjs/loader:1205:27) 在Module._extensions..js (节点:internal/modules/cjs/loader:1295:10) 在 Module.load (node:internal/modules/cjs/loader:1091:32) 在 Module._load (node:internal/modules/cjs/loader:938:12) 在 Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:83:12) 在节点:internal/main/run_main_module:23:47
节点.js v20.9.0
我做了什么:
制作了一个并添加了:package.json
"type": "module"
{
"name": "new-folder",
"version": "1.0.0",
"description": "",
"main": "index.js",
"type": "module",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}
SyntaxError 已得到纠正,但为什么在我遇到错误时它能正常工作?
答: 暂无答案
评论