Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

console.log('hello')

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')"
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading