console.log not working

69 views
Skip to first unread message

noderiety

unread,
Dec 29, 2014, 2:12:56 AM12/29/14
to nod...@googlegroups.com
Hello, I just finished installing nodejs on ubuntu. I wanted to do a simple Hello World to get started so I executed the following at the command prompt:

    node console.log('hello world')

However, this returns the following error:

    -bash: syntax error near unexpected token '('

Any idea what might be causing this issue and how to fix?

Aria Stewart

unread,
Dec 29, 2014, 10:11:01 AM12/29/14
to nod...@googlegroups.com
Yes! You're actually mixing up two different things. The node command-line consists of a script to run, and arguments to pass to it, parsed by the shell -- the shell is what's giving you the error, because you're giving it javascript, not simple space-separated arguments like command-lines take. (you can do more, but quoting rules abound!)

So what you want to do is put your one-line script in a file -- call it 'hello.js' perhaps, then:

node hello.js

It should print out "hello world"

Aria

Peter Petrov

unread,
Dec 29, 2014, 8:04:41 PM12/29/14
to nod...@googlegroups.com
An alternative solution:

echo "console.log('hello world')" | node

Paul Spaulding

unread,
Dec 30, 2014, 9:17:42 AM12/30/14
to nod...@googlegroups.com
node -e "console.log('hello world')"

Paul Spaulding

unread,
Dec 30, 2014, 9:21:28 AM12/30/14
to nod...@googlegroups.com
or, evaluate and print result:

node -p "'hello world'"



On Monday, December 29, 2014 2:12:56 AM UTC-5, noderiety wrote:
Reply all
Reply to author
Forward
0 new messages