node.js - 语法错误 : Invalid or Unexpected Token when running a nodejs file using Visual Studio Code (Using powershell)

标签 node.js powershell

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

This is the image of my screen

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

关于node.js - 语法错误 : Invalid or Unexpected Token when running a nodejs file using Visual Studio Code (Using powershell),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64222605/

相关文章:

javascript - ajax调用未正确发送数据

powershell - 无法与foreach对象并行开始作业

c# - .NET 中 PowerShell 的引用是什么?

powershell - 如何在powershell中允许手动或自动选择?

javascript - Android + Linux Chroot + Node.js = 在手机上运行您的 node.js 应用程序

ruby-on-rails - 如何正确安装 dc.js 以便与 Rails 5 一起使用?

javascript - 当连接到 MongoDB Replicaset 时,Node.js "auth fail"仅 3 个服务器中的 2 个处于运行状态

node.js - sendFile 返回空对象 Express postman

git - SSH 注册机 "no such file or directory"

python - 使用 Popen 在 Python 中执行 Powershell 脚本,如何获取 Powershell 脚本的输出并将其更新到网页?