2014년 12월 29일 월요일

console.log not working

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?



> 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?
>
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"



An alternative solution:

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



or, evaluate and print result:

node -"'hello world'"



node -"console.log('hello world')"


댓글 없음:

댓글 쓰기