Getting an 'Uncaught SyntaxError: import declarations may only appear at top level of a module' error with Hangman Game code

huangapple go评论81阅读模式
英文:

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:

  1. Is there another way to fix the syntax error about "import declarations".
  2. 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:

  1. Is there another way to fix the syntax error about "import declerations".
  2. 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 &lt;script type=&#39;module&#39;&gt; you can omit this syntax or use compiler (esbuild, webpack...) if you don't want to use type=&#39;module&#39;

  • You get hangman is undefined error because hangman is declared in main.js and main.js is imported as a module so it is local please include functions like logAndCreateButton in main.js or in main.js use window.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 &lt;script&gt; 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

huangapple
  • 本文由 发表于 2023年2月10日 15:11:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/75407942.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定