'Is it possible to run node.js script which is not in file?

I need to run javascript code using node.js from C++ application, but I don't want to save code to file before. Is it anyway possible to forward code to node.exe directly, without temporary save it to file?



Solution 1:[1]

yes it is. You can use the --eval flag to run code:

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

Solution 2:[2]

Well you can launch node.js and then stuff the script on stdin. How to do that exactly depends on your platform.

For details google how to fork process and get it's stdin with your platform and libraries.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Johannes Merz
Solution 2 Tomáš Zato - Reinstate Monica