令牌"/>
语法错误:使用 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 文件时出现无效或意外令牌
发布评论