簡體   English   中英

如何在 Visual Studio Code 中運行這個 JavaScript 蛇游戲?

[英]How can I run this JavaScript snake game in Visual Studio Code?

我有一個用 JavaScript 編寫的 Snake 游戲,當我通過單擊屏幕右上角的運行圖標來運行 game.js 文件時,出現此錯誤。

[Running] node "/Users/matthewrowlinson/Documents/Javascript Code/snake/game.js"
(node:79394) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
(Use `node --trace-warnings ...` to show where the warning was created)
/Users/matthewrowlinson/Documents/Javascript Code/snake/game.js:1
import { update as updateSnake, draw as drawSnake, SNAKE_SPEED, getSnakeHead, snakeIntersection } from './snake.js'
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at wrapSafe (internal/modules/cjs/loader.js:1001:16)
    at Module._compile (internal/modules/cjs/loader.js:1049:27)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
    at Module.load (internal/modules/cjs/loader.js:950:32)
    at Function.Module._load (internal/modules/cjs/loader.js:790:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:76:12)
    at internal/main/run_main_module.js:17:47

[Done] exited with code=1 in 0.065 seconds

這很有趣,因為我不應該收到此錯誤,因為我在 html 文件中專門設置了 type="module"。

<script src="game.js" defer type="module"></script>

到目前為止,這是我將類型設置為“模塊”的唯一方法,我不知道為什么它什么也不做,我仍然遇到相同的錯誤。

我必須假設您正在嘗試托管節點腳本,並且“game.js”腳本通過<script src="game.js" defer type="module"></script>包含在 html 文件中<script src="game.js" defer type="module"></script> .

“導入”選項僅適用於服務器端節點腳本。 我假設當您連接到服務器時,您希望在瀏覽器上彈出 game.js 和 html 文件。 這意味着 game.js 將在客戶端運行,在那里你不能有“節點模塊”或“導入”。

要在 html 中包含 js 腳本,您不需要指定“延遲類型”或任何模塊內容。

翻譯:博士

你可以只做<script src="game.js" >

要么

如果您不需要托管服務器,請執行<script src="game.js"></script>

我無法做出真正好的判斷,因為我需要假設很多事情。 還有更多信息嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM