获取Microsoft JScript编译错误

6

我运行了一段代码,使用的是first.js

var http = require("http");

http.createServer(function (request, response) {

   // Send the HTTP header
   // HTTP Status: 200 : OK
   // Content Type: text/plain
   response.writeHead(200, {'Content-Type': 'text/plain'});

   // Send the response body as "Hello World"
   response.end('Hello World\n');
}).listen(8124);

// Console will print the message
console.log('Server running at http://127.0.0.1:8124/');

当我用Git Bash运行它时,它会给出期望的输出。但是在Node.js命令提示符下,它会给出Microsoft JScript编译错误。

1
Node.js与JScript无关,因此您正在使用错误的解释器运行脚本。 - Anatol - user3173842
3个回答

21

看起来你刚刚在终端窗口输入了脚本的名称,因此要求Windows查找适当的解释器来解释你的脚本。你应该尝试使用nodejs来解释脚本:

node your-script.js

不仅仅是

your-script.js

2
这里的问题可能来自多个方面。最有可能是安装相关的问题。
以下代码(引自上文):
var http = require("http");
http.createServer(function (request, response) {
response.writeHead(200, {'Content-Type': 'text/plain'});
response.end('Hello World\n'); }).listen(8124);
console.log('Server running at http://127.0.0.1:8124/');

当我尝试在浏览器中加载它时,结果是积极的。因此,我建议以下措施:
1. 将代码片段窗口中的内容复制到全新的first.js文件中,并在命令行中导航到该目录,使用命令"Node first.js"(自然不带引号)运行该文件。 1.1 如果使用除纯文本编辑器(例如notepad ++,notepad,sublime等)之外的任何编辑器,请确保将文件设置为输出纯文本(这是一个愚蠢的错误,但经常被忽视)。
2. 重新安装node(值得一试) 3. 如果所有其他方法都失败且您已安装npm,则输入“npm install http”,然后重新运行first.js应用程序。

0

我在我的nodejs应用程序中一直遇到同样的错误,后来发现我只写了“index.js”(package.json文件上的脚本文件名)。所以每当我运行命令npm start时,它都会给我报错。我已经在我的应用程序上安装了nodemon,所以我将其更改为nodemon index.js,之后它就完美地工作了。我在这里附上截图:我在package.json文件上更改的代码


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接