17

I want to temporarily run the node javascript. For example, like this:

echo console.log(1+1) > tmp.js & node tmp.js & del tmp.js

It works fine. However, I do not like the use of temporary files. Is there any other cleaner way?

For example, I wonder if there is an option for such a function in node.exe

3
  • 1
    You can use the -e, --eval option: node -e "console.log(1+1)" Documentation: nodejs.org/api/cli.html#cli_e_eval_script Commented Jul 16, 2021 at 2:54
  • 1
    @mdker Write that as an answer Commented Jul 16, 2021 at 3:08
  • In general you don't need temporary files for things like this on Linux. Node, like any sane scripting language, enters a REPL when invoked without a file. Therefore you can pipe commands to it: echo 'console.log(1+1)' | node. This works for other REPL interpreters like bash, tcl, ksh, python etc. Commented Jul 16, 2021 at 3:11

1 Answer 1

31

You can use the -e, --eval option: node -e "console.log(1+1)"

Documentation: https://nodejs.org/api/cli.html#cli_e_eval_script

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.