Execute Javascript using Node in terminal

I know that

Node.js allows to write JavaScript code that runs directly in a
computer process itself instead of in a browser

Reading this, I try to open my terminal and run:


but I get zsh: unknown file attribute.
The same happens with node console.log('hello').

If I instead create a file hello.js and put the console inside it and then I run:

node src/hello.js

it works. Why? What am I missing?

Sorry for the (maybe) stupid question

>Solution :

It’s because the console.log('hello') is being evaluated by your shell (zsh) before running the command and it has no idea what console.log('hello') is. You could use the -e or --eval arguments.

$ node -e "console.log('hello')"

Leave a Reply