Node REPL抛出SyntaxError: Unexpected identifier错误

3

我对node.js非常新手。在node REPL中一切都很正常。但是出现了一些变化。当我尝试执行一个文件时,它显示如下内容...

D:\Projects-2015\uniqueva>node
/>node module.js


SyntaxError: Unexpected identifier
    at Object.exports.createScript (vm.js:44:10)
    at REPLServer.defaultEval (repl.js:117:23)
    at bound (domain.js:254:14)
    at REPLServer.runBound [as eval] (domain.js:267:12)
    at REPLServer.<anonymous> (repl.js:279:12)
    at REPLServer.emit (events.js:107:17)
    at REPLServer.Interface._onLine (readline.js:214:10)
    at REPLServer.Interface._line (readline.js:553:8)
    at REPLServer.Interface._ttyWrite (readline.js:830:14)
    at ReadStream.onkeypress (readline.js:109:10)

即使我尝试这样做,仍然会出现这种情况...
node --version

我在module.js中有以下代码...
var http = require("http");

http.createServer(function(request, response){
    response.writeHead(200, {"Content-Type" : "text/plain"});
    response.write("Hello World");
    response.end();
}).listen(8888);

即使我只尝试控制台输出某些内容,或者干脆将文件留空,也会有同样的问题。

在此输入图片描述

你的 module.js 文件里有什么内容? - Osukaa
@Osukaa,我已经更新了我的问题。 - Smoking Sheriff
2个回答

3
你不能在node REPL中调用node可执行文件,只能使用JavaScript语句/表达式。
尝试使用require('./module.js')

实际上我正在使用Git Bash来执行Node脚本。这会有影响吗?几天前它还能正常工作! - Smoking Sheriff
你可能没有进入节点REPL。在REPL内部没有理由它会起作用... - Madara's Ghost
好的,那我该怎么做呢?我该如何在Git bash中执行它?我手动删除了几个npm模块。 - Smoking Sheriff
2
不要执行 node,只需执行 node module.js - Madara's Ghost

1

你是通过双击来运行 node 吗?

你需要在命令行中使用命令 "node xyz.js" 来运行 node。

node 应该在你的类路径中,或者在执行 node 时使用完整路径,例如 c:\work\nodetest\node .\xyz.js

在上面的例子中,我将 js 文件复制到与 node 文件相同的文件夹中。


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