javascript 语法错误:createScript 中的令牌无效或意外 (vm.js:80:10)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/50203953/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 08:58:29  来源:igfitidea点击:

SyntaxError: Invalid or unexpected token at createScript (vm.js:80:10)

javascriptnode.jsexpress

提问by l--''''''---------''''''''''''

I've just arted to learn node, and when executing a very simple app from a powershell terminal:

我刚刚开始学习 node,并在从 powershell 终端执行一个非常简单的应用程序时:

node app.js

I am getting the following exception:

我收到以下异常:

SyntaxError: Invalid or unexpected token
    at createScript (vm.js:80:10)
    at Object.runInThisContext (vm.js:139:10)
    at Module._compile (module.js:607:28)
    at Object.Module._extensions..js (module.js:654:10)
    at Module.load (module.js:556:32)
    at tryModuleLoad (module.js:499:12)
    at Function.Module._load (module.js:491:3)
    at Function.Module.runMain (module.js:684:10)
    at startup (bootstrap_node.js:187:16)
    at bootstrap_node.js:608:3
PS C:\Users\kisco\Source\Repos\library> node app.js
C:\Users\kisco\Source\Repos\library\app.js:1
(function (exports, require, module, __filename, __dirname) { ??v
                                                              ^

SyntaxError: Invalid or unexpected token
    at createScript (vm.js:80:10)
    at Object.runInThisContext (vm.js:139:10)
    at Module._compile (module.js:607:28)
    at Object.Module._extensions..js (module.js:654:10)
    at Module.load (module.js:556:32)
    at tryModuleLoad (module.js:499:12)
    at Function.Module._load (module.js:491:3)
    at Function.Module.runMain (module.js:684:10)
    at startup (bootstrap_node.js:187:16)
    at bootstrap_node.js:608:3
PS C:\Users\kisco\Source\Repos\library>

Here's my source for app.js:

这是我的来源app.js

var express = require('express');

var app = express();

app.get('/', function(req, res){
    res.send('Hello from my libvrary app')
})

app.listen(3000, function(){
    console.log('listening on port 3000');
});

What am I doing wrong?

我究竟做错了什么?

回答by George Bailey

It looks like there is some encodingissue with the file that you're trying to run with Node. Sometimes, files created with some command line utility does some encoding issues with the file. So, I would suggest not to use them to create file. Just create file with Right-Click > New File > app.jsand replace your old file with this app.js. And try running node app.js.

它看起来像有一些encoding问题与您正在试图与运行该文件Node。有时,使用某些命令行实用程序创建的文件会对文件产生一些编码问题。所以,我建议不要使用它们来创建文件。只需用它创建文件Right-Click > New File > app.js并用它替换旧文件即可app.js。并尝试运行node app.js

回答by aiyu

I had a similar issue. My problem was that I copy-pasted code from a website and the '"' were wrongly formatted by my editor. So, I replaced all those quotations with " or ', whichever is applicable.

我有一个类似的问题。我的问题是我从网站复制粘贴了代码,而我的编辑器错误地格式化了 '"'。因此,我将所有这些引号替换为 " 或 ',以适用者为准。

That solved my issue.

那解决了我的问题。

回答by SofienM

I also had the same issue. And indeed it was an encoding problem.
Our js file was parsing another file which had been copied/pasted from a pdf. Turned out there was a corrupted space character in the middle of the file.

我也有同样的问题。确实这是一个编码问题。
我们的 js 文件正在解析另一个从 pdf 复制/粘贴的文件。结果发现文件中间有一个损坏的空格字符。

Removing it solved my issue.

删除它解决了我的问题。

回答by lightcode4

In my case, all I had to do was unistall webstorm (in your case it might be whatever text editor it is you are using for your Repository to work on your project), delete my project folder and install webstorm afresh then running the following after getting my Repository from github

就我而言,我所要做的就是卸载 webstorm(在您的情况下,它可能是您用于存储库处理项目的任何文本编辑器),删除我的项目文件夹并重新安装 webstorm,然后运行以下命令从 github 获取我的存储库

npm init 
npm install mysql
npm install colors

and then node index.jsand I got my server running up again.

然后节点index.js和我让我的服务器再次运行。

Note that I had to update my NodeJS to the latest version.

请注意,我必须将 NodeJS 更新到最新版本。

回答by Hudson Peden

I got this same error when trying to run a program I compiled as an executable. Instead of just running ./programI was trying to run it with node. So I did node ./program.

尝试运行我编译为可执行文件的程序时,我遇到了同样的错误。./program我试图用 node.js 运行它,而不是仅仅运行它。所以我做了node ./program

Trying to run the executable (compiled with pkg for linux) with node gave me pretty much the exact same error you got here.

尝试使用 node 运行可执行文件(使用 pkg for linux 编译)给了我与您在此处遇到的几乎完全相同的错误。