admin管理员组文章数量:1323732
I have a javascript file hello.js with console.log("Hello World")
. I want to run it from my terminal (I am on a mac). I have node installed and I take the following steps -
- Open terminal and navigate to the directory where hello.js exists.
- Run mand "node hello.js"
But I dont see the console statement (Hello World) in my terminal.
Can some one please help me run javascript from terminal (or tell me what I am doing wrong) ?
PS: I have checked this thread but my problem still exists.
I have a javascript file hello.js with console.log("Hello World")
. I want to run it from my terminal (I am on a mac). I have node installed and I take the following steps -
- Open terminal and navigate to the directory where hello.js exists.
- Run mand "node hello.js"
But I dont see the console statement (Hello World) in my terminal.
Can some one please help me run javascript from terminal (or tell me what I am doing wrong) ?
PS: I have checked this thread but my problem still exists.
Share Improve this question edited Feb 16, 2018 at 21:38 pwolaq 6,38121 silver badges45 bronze badges asked Feb 16, 2018 at 21:20 NewBeeNewBee 4094 gold badges11 silver badges18 bronze badges 14-
3
So when you type
node hello.js
you get no error, nothing at all from your terminal.? – Keith Commented Feb 16, 2018 at 21:22 -
Are you sure you have node installed? You can double check that node is installed by doing
node -v
to get its version. Are you absolutely certain you are in the correct directory before runningnode hello.js
? – Moose Commented Feb 16, 2018 at 21:25 -
What is your output if you run
node -v
– Ben Mohorc Commented Feb 16, 2018 at 21:26 -
1
What does running
cat hello.js
output? – Patrick Roberts Commented Feb 16, 2018 at 21:35 -
1
Check
which node
as well... – Patrick Roberts Commented Feb 16, 2018 at 21:49
4 Answers
Reset to default 2One possible error is that your JavaScript file doesn't end with a new line character.
The node parser will read the last line, but it won't pletely process the line unless it includes a new line marker (or, possibly, a semicolon).
Make sure your file includes the new line (as well as, preferably, a semicolon), i.e.:
console.log("Hello World");
// EOF ment, to validate a new line marker after last line of code.
This might solve your issue - unless the reason for your issue lies somewhere else.
Good luck!
You have to first accurately create the path to the .js file. This is probably your problem.
When you first open the CLI (Command Line) there will be a path displayed. For example...
C:\Users\yourname>
from there you have to get to where the file is located, so you type in.. C:\Users\yourname>cd\Users\yourname\Documents\course\jsfolder
cd means "change directory"..and just put the full path to where the file is located.
Now to see if you are there by typing dir on the end
C:\Users\yourname>\Documents\course\jsfolder>dir
This will list all the files in that last directory. You should see your New.js file in there. You are now ready to go!
Follow what Anupam said above...type.. node New.js ..and your file will run if you have node.js installed on your puter..BINGO!>
one last thing ..I believe the mand "node" is "$node" on an Apple
There are a few additional solutions you could use other than node.
The expected behavior of node would be to print "Hello World" for you, and there has been help troubleshooting in the ments of your post.
jsc is included with macOS. You should make a link to the file first as described in this post, and change your console.log()
methods to debug()
.
rhino is available from Mozilla. It would require you to switch your console.log()
methods to print()
.
For installing rhino, you may be able to avoid some of the issues you've had with node by installing through homebrew. As a last means of troubleshooting node, you may also find it useful to reinstall it through homebrew.
You can Run your JavaScript File from your Terminal only if you have installed NodeJs runtime. If you have Installed it then Simply open the terminal and type “node FileName.js”.
- Open Terminal or Command Prompt.
- Set Path to where File is Located (using cd).
- Type “node New.js” and Click Enter
本文标签: nodejsRun Javascript file from command lineStack Overflow
版权声明:本文标题:node.js - Run Javascript file from command line - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742121267a2421712.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论