英文:
Getting an 'Uncaught SyntaxError: import declarations may only appear at top level of a module' error with Hangman Game code
问题
I am learning web development and trying to use Vanilla JS to create a Hangman Game. The game runs as expected with a words array in the main.js file but when I import words from a different file, Browser shows the error "Uncaught SyntaxError: import declarations may only appear at the top level of a module".
I fixed this by specifying type = "module" in the index.html file but now, I am getting a reference error: "Uncaught ReferenceError: hangman is not defined" (hangman is an instance of the Hangman object).
I tried adding a function and calling it using the load eventListener, but the issue persisted.
I am primarily wondering about:
- Is there another way to fix the syntax error about "import declarations".
- Why am I getting the reference error even though I have set 'hangman' as a global variable in main.js?
P.S - I am not sure about how much info to add so here is the GitHub link for main.js and index.html. I will appreciate feedback on my question to make sure I ask questions more efficiently from the next time.
英文:
I am learning web development and trying to use Vanilla JS to create a Hangman Game. The game runs as expected with a words array in the main.js file but when I import words from a different file, Browser shows the error "Uncaught SyntaxError: import declarations may only appear at top level of a module".
I fixed this by specifying type = "module" in the index.html file but now, I am getting reference error: "Uncaught ReferenceError: hangman is not defined" (hangman is an instance of the Hangman object).
I tried adding a function and calling it using the load eventListener but the issue persisted.
I am primarily wondering about:
- Is there another way to fix the syntax error about "import declerations".
- Why am I getting the reference error even though I have set 'hangman' as a global variable in main.js?
P.S - I am not sure about how much info to add so here is the github link for main.js and index.html. I will appreciate feedback on my question to make sure I ask questions more efficiently from the next time.
答案1
得分: 0
-
因为你使用了JavaScript模块语法,所以你必须声明
<script type='module'>
,如果你不想使用type='module'
,你可以省略这个语法或者使用编译器(如esbuild、webpack...)。 -
你会得到一个"hangman is undefined"错误,因为"hangman"是在"main.js"中声明的,而"main.js"被导入为一个模块,所以它是局部的。请将类似"logAndCreateButton"这样的函数包含在"main.js"中,或者在"main.js"中使用"window.hangman = hangman"(不推荐)来解决这个问题。
英文:
-
Because you use javascript module syntax you have to declare
<script type='module'>
you can omit this syntax or use compiler (esbuild, webpack...) if you don't want to usetype='module'
-
You get
hangman is undefined
error becausehangman
is declared inmain.js
andmain.js
is imported as amodule
so it is local please include functions likelogAndCreateButton
inmain.js
or inmain.js
usewindow.hangman = hangman
(not recommended) to solve the problem
答案2
得分: 0
在查看代码后,我发现错误是由于定义顺序引起的。在index.html
中有一个<script>
标签,您在其中定义了加载时调用hangman.reset()
的点击事件,但在该脚本的作用域内未定义hangman
。将脚本标签中的所有内容移动到main.js
文件中,现在hangman
实例位于logAndCreateButton
函数的作用域内,这应该解决您的问题。
简而言之:将位于index.html
中的脚本的内容移动到main.js
中。
英文:
After taking a look at the code, I found the error is being caused beacuse of the defining order, inside index.html
there is a <script>
tag that you define your on load that on click calls hangman.reset()
but hangman
inside the scope of that script is not defined, by moving all of the content of the script tag into the main.js
file now the hangman
instance is in scope of the logAndCreateButton
function and that should fix your issue.
TLDR: move the contents of the script that is inside index.html
into main.js
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论