[英]Run simple js file using node
我正在使用節點v0.10.20和OSX 10.8.5。
我創建了一個簡單的js文件,名為variables.js,您可以在下面看到:
var productName
var currentPrice
var totalCost
var productTax
productName = "cookies"
currentPrice = 3
productTax = currentPrice * .07
totalCost = currentPrice + productTax
console.log("Your " + productName + " cost $" + totalCost)
當我分別在節點repl或chrome中運行這些行時,它可以正常工作,輸出是“您的cookie花費$ 3.21”。
但是,當我從文件所在的目錄運行命令“ node variables.js”時,出現以下錯誤消息:
/directorypath/variables.js:13
# create a javascript file (variables.js) that
^
SyntaxError: Unexpected token ILLEGAL
at Module._compile (module.js:439:25)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:901:3
我在這里做錯了什么?
您的錯誤在第13行,但是您粘貼的代碼段只有10行代碼。 如果希望我們能夠為您提供盡可能多的幫助,則可能需要發布整個腳本。
從錯誤中,我看到您正在使用#
來創建我認為是注釋行的內容。 即使可以從終端運行node.js
腳本,它仍然是Javascript而不是bash。 因此,您仍然使用//
注釋一行,或使用/* */
注釋塊。 這是兩個示例:
// I'm a Javascript comment!
/* and all of this
text is as well!
*/
這是帶有適當注釋的代碼:
var productName
var currentPrice
var totalCost
var productTax
productName = "cookies"
currentPrice = 3
productTax = currentPrice * .07
totalCost = currentPrice + productTax
console.log("Your " + productName + " cost $" + totalCost)
// Commented stuff would go here
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.