简体   繁体   English

无法在终端 Mac 中运行 javascript 程序

[英]cannot run javascript program in terminal Mac

I have made a JavaScript hello world program in BBEdit, and saved it as app.js.我在BBEdit中做了一个JavaScript的hello world程序,保存为app.js。 But when I'm trying to run the file in Terminal, I keep getting the error (syntax error near unexpected token `'Hello world'').但是当我尝试在终端中运行该文件时,我不断收到错误消息(意外标记“Hello world”附近的语法错误)。

The code:代码:

console.log("Hello world")

The odd thing is, when I type node and then console.log("Hello world") into terminal, it works, just not when placing app.js directly into terminal.奇怪的是,当我在终端中键入 node 然后console.log("Hello world")时,它起作用了,只是在将 app.js 直接放入终端时不起作用。

What am I doing wrong?我究竟做错了什么?

It seems that you are attempting to run the JavaScript as if it was a shell script (dragging app.js on to Terminal).似乎您正在尝试运行 JavaScript,就好像它是 shell 脚本一样(将 app.js 拖到终端)。

What you will have to do instead is run the JavaScript file with Node.您需要做的是使用 Node.js 运行 JavaScript 文件。

Open Terminal and then type:打开终端然后输入:

node app.js

Make sure you have the correct path to find your JavaScript file and it should work as you expect.确保您有正确的路径来找到您的 JavaScript 文件,并且它应该按您预期的方式工作。

Make sure your system knows where node is, of course.当然,请确保您的系统知道节点在哪里。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM