语法错误:使用 Visual Studio Code(使用 powershell)运行 nodejs 文件时出现无效或意外令牌

编程入门 行业动态 更新时间:2024-10-04 05:26:55

语法错误:使用 Visual Studio Code(使用 powershell)运行 nodejs 文件时出现无效或意外<a href=https://www.elefans.com/category/jswz/34/1771317.html style=令牌"/>

语法错误:使用 Visual Studio Code(使用 powershell)运行 nodejs 文件时出现无效或意外令牌

我一直在寻找解决方案,但似乎找不到。我是 nodejs 的初学者,基本上我在名为“HelloNode”的目录中创建了一个名为“app.js”的新 nodejs 文件。当我在 powershell 中使用“node app.js”时,终端告诉我:

VS 代码中的代码:

var msg = "hello world";
console.log(msg);

终端输出:

PS C:\Users\yanab\HelloNode> node app.js
C:\Users\yanab\HelloNode\app.js:1
��v


SyntaxError: Invalid or unexpected token
    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:791:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
    at internal/main/run_main_module.js:17:47

我尝试过的解决方案:

  • 试图改变环境变量
  • 使用 nvm use 'version number' 使用 nodejs 正确版本

备注:

  • 结合使用 Visual Studio Code 和 nvm
  • 搜索了一段时间后,它可能与 UTF-16 编码有关,但它不是 UTF-8 编码,但我可能错了。

解决方案: 好吧,我基本上只是通过右键单击 -> 新文件在 vscode 中创建文件,它就可以工作了。我之前所做的是使用 powershell 和命令 echo 来创建一个新文件。不明白发生了什么,但在 vscode 中制作文件似乎是解决方案。

回答如下:

我有同样的情况。事实证明该文件是 UCS-2 编码的。使用notepad ++将其转换为UTF-8后,它开始正常工作了。

就我而言,使用 UCS-2 编码的原因是我填写文件的方式: echo 'console.log("空心世界");' > 测试.js

更多推荐

语法错误:使用 Visual Studio Code(使用 powershell)运行 nodejs 文件时出现无效或意外令牌

本文发布于:2024-05-30 19:11:13,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1770825.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:令牌   意外   语法错误   文件   Visual

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!