簡體   English   中英

Hello World示例的Node.js錯誤

[英]Node.js Error for Hello World example

我是node.js的新手,我剛剛將其安裝在Windows機器上。 實際上,我正在遵循有關tutorialspoint的教程。 安裝后,我被告知要創建一個main.js文件,並將以下代碼放入文件中。

/* Hello, World! program in node.js */
console.log("Hello, World!")

我通過輸入$ node main.js使用Node.js解釋器執行了main.js文件,但是出現以下錯誤。

SyntaxError: Unexpected identifierat Object.exports.createScript      
(vm.js:24:10)
at REPLServer.defaultEval (repl.js:221:25)
at bound (domain.js:280:14)
at REPLServer.runBound [as eval] (domain.js:293:12)
at REPLServer.<anonymous> (repl.js:412:12)
at emitOne (events.js:82:20)
at REPLServer.emit (events.js:169:7)
at REPLServer.Interface._onLine (readline.js:210:10)
at REPLServer.Interface._line (readline.js:549:8)
at REPLServer.Interface._ttyWrite (readline.js:826:14)

請幫幫我。 謝謝。

聽起來像您在REPL (讀取-評估-打印循環)中。 嘗試按ctrl + c幾次,看看是否退出命令提示符。 然后嘗試運行node main.js 您應該看到所需的輸出。

我認為,您不是從shell運行node main.js ,而是從REPL運行節點。

您不需要先運行node

$ cat main.js
console.log("Hello, World!")
$ node main.js
Hello, World!

嗯,您在Windows上。 然后,您應該在cmd.exe中執行以下操作:

c:\...> cd c:\projects\hello
c:\...> type main.js
console.log("Hello, World!")
c:\...> node main.js
Hello, World!

注意:上面的cattype命令是多余的,僅用於文件內容演示。


另外,當您使用nodejs REPL ,可以直接編寫javascript代碼。
試一試:

> console.log('Hey');
'Hey'
undefined
> require('./main.js');
Hello, World!
undefined
> exit
Bye-bye

在測試程序之前不要運行node

您可以使用Windows 7的常規命令提示符從任何位置運行節點,但必須指定具有確切文件路徑的js文件名。

例如:c:\\ users [您的名字]>節點d:\\ projects \\ js_files \\ main.js

如果您在執行任何操作之前已設置了path =“ c:\\ progam files \\ nodejs \\ bin”環境變量,那么它將起作用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM