Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to execute code from Node.js server command line? [duplicate]

I'm just starting with Node.js but I have experience with client-side JavaScript and PHP/Apache.

In your browser you can execute JS code from the developer console at any time. You can manipulate global vars and call global functions and methods. If I call console.log('Hello world!') from the browser's developer console, it will log the message to the console I'm using, just like in a script you write.

In Node.js when you call console.log('Hello world!') from within your "app", the result is logged as you'd expect, just like JS on the client-side. Can I do what I would do on the client-side?

My question is can I execute my own JS code from the command line like I would on the client-side, manipulating vars (such as the HTTP server, filesystem, etc.) and calling functions that are available in my app? I don't care why, I just want to know if I can do it.

like image 473
Jared Avatar asked May 21 '26 13:05

Jared


1 Answers

V8 comes with an extensive debugger which is accessible out-of-process via a simple TCP protocol. Node has a built-in client for this debugger. To use this, start Node with the debug argument; a prompt will appear:

Node's debugger client doesn't support the full range of commands, but simple step and inspection is possible. By putting the statement debugger; into the source code of your script, you will enable a breakpoint.

http://nodejs.org/api/debugger.html

Yes this is possible. You just need to write the statement debugger wherever you want your code to stop and you want to use the command line (and start node with the debug argument, eg: node debug app.js

like image 173
edi9999 Avatar answered May 23 '26 02:05

edi9999