简体   繁体   English

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

[英]Syntax Error: Invalid or Unexpected Token when running a nodejs file using Visual Studio Code (Using powershell)

I've been trying to look for a solution but I can't seem to find one.我一直在寻找解决方案,但似乎找不到。 I am a beginner to nodejs and basically I have created a new nodejs file called "app.js" inside of a directory called "HelloNode".我是 nodejs 的初学者,基本上我在名为“HelloNode”的目录中创建了一个名为“app.js”的新 nodejs 文件。 When I use "node app.js" inside of powershell, the terminal tells me this:当我在 powershell 中使用“node app.js”时,终端告诉我:

This is the image of my screen这是我的屏幕图像

Code in VS Code: VS Code 中的代码:

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

Output in terminal:终端输出:

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

Solutions I have tried:我尝试过的解决方案:

  • Tried to change environment variable尝试更改环境变量
  • Used the nodejs correct version using nvm use 'version number'使用 nvm use 'version number' 使用 nodejs 正确版本

Notes:笔记:

  • Using a combination of Visual Studio Code and nvm使用 Visual Studio Code 和 nvm 的组合
  • After searching for a while it might have something to do with UTF-16 encoding and how it isn't in UTF-8 encoding but I may be wrong.搜索了一段时间后,它可能与 UTF-16 编码以及它如何不在 UTF-8 编码中有关,但我可能是错的。

Solution: Well I basically just made the file inside of vscode by right click -> new file and it worked.解决方案:好吧,我基本上只是通过右键单击 -> 新建文件在 vscode 中创建了文件并且它起作用了。 What I did before was use powershell and the command echo to make a new file.我之前所做的是使用 powershell 和命令 echo 来创建一个新文件。 Don't understand what happened but making the file inside of vscode seems to be the solution.不明白发生了什么,但在 vscode 中制作文件似乎是解决方案。

same problem here.同样的问题在这里。 I created file via powershell echo command.我通过 powershell echo 命令创建了文件。 My solution is reopen my computer and create file without command line.我的解决方案是重新打开我的计算机并在没有命令行的情况下创建文件。 :$ :$

This is an error that appears when there is some enexpected syntax when running javascript.这是在运行 javascript 时出现一些意外语法时出现的错误。

at wrapSafe (internal/modules/cjs/loader.js:1001:16)
at Module._compile (internal/modules/cjs/loader.js:1049:27)

This second line says that this error appeared while compiling the code file.第二行表示编译代码文件时出现此错误。 (even though javascript is not a compiled language). (即使 javascript 不是编译语言)。 The same error appears for the following code(it wont even console log):以下代码出现相同的错误(它甚至不会控制台日志):

console.log('running code');
2q307qwekfjn;

So there is some error in the formatting of your file, some unexpected symbol(the ??v symbol) added by the ide or when saving etc.因此,您的文件格式存在一些错误,ide 或保存时添加了一些意外符号(??v 符号)。

I had the same situation.我有同样的情况。 It turns out the file was UCS-2 encoded.结果证明该文件是 UCS-2 编码的。 After converting it to UTF-8 using notepad ++, it started working fine.使用记事本++将其转换为UTF-8后,它开始正常工作。

In my case, the reason for the UCS-2 encoding was the way I was filling out the file: echo 'console.log("Hollow world");'就我而言,UCS-2 编码的原因是我填写文件的方式: echo 'console.log("Hollow world");' > test.js > 测试.js

问题在于语法,'' 替换为 '',通常很少有像苹果笔记程序这样的编辑器使用 ' 而不是 ',当你复制粘贴时,它不会编译。

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

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