简体   繁体   English

如何在 Visual Studio Code 中运行这个 JavaScript 蛇游戏?

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

I have this Snake game written in JavaScript and when I go to run the game.js file by clicking the run icon on the top right part of the screen I get this error.我有一个用 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

Which is funny because I shouldn't get this error because I specifically set the type="module" in the html file.这很有趣,因为我不应该收到此错误,因为我在 html 文件中专门设置了 type="module"。

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

So far this is the only way I have seen as an answer for this error, setting the type to "module" and I don't know why it does nothing and I'm still getting the same error.到目前为止,这是我将类型设置为“模块”的唯一方法,我不知道为什么它什么也不做,我仍然遇到相同的错误。

I'll have to assume that you are trying to host a node script, and the "game.js" script is included in the html file via <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> .

the "import" option only works on the server side node script. “导入”选项仅适用于服务器端节点脚本。 I'm assuming that when you connect to the server, you want game.js and the html file to pop up on your browser.我假设当您连接到服务器时,您希望在浏览器上弹出 game.js 和 html 文件。 This means that the game.js will be running on the client side, where you can't have "node modules" or "imports".这意味着 game.js 将在客户端运行,在那里你不能有“节点模块”或“导入”。

To include a js script in html, you don't need to specify "defer type" or any of the module stuff.要在 html 中包含 js 脚本,您不需要指定“延迟类型”或任何模块内容。

TL:DR翻译:博士

you can just do <script src="game.js" >你可以只做<script src="game.js" >

OR要么

if you don't need to host a server, do <script src="game.js"></script>如果您不需要托管服务器,请执行<script src="game.js"></script>

I can't make a really good judgement because I need to assume a lot of things.我无法做出真正好的判断,因为我需要假设很多事情。 Is there any more information?还有更多信息吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM