node.js attach REPL to script - linux

How do I start a node.js script and still be able to execute commands into the terminal ? I am looking for a node.js REPL that is also there for my custom script, so that I can inspect/log the state of my program for instance.
This is something similar to this JVM question, but for node.js.
I have tried node -i server.js without results. Do I need to have custom code in my script or is it feasible without that ? I saw this post, but it requires custom code, which I'd like to avoid.
Also, bonus points for reattaching a node script launched by an init script (I can see it in the process list : node -i server.js).

You can start a repl loop from within your program
http://nodejs.org/api/repl.html

Does the other way round work for you?
Start the REPL and then load the script and then execute your commands. Use load to load your script.
Inside REPL, try
.load server.js

Related

Inconsistencies between node's `spawn` and terminal

I'm trying to run a script from node, and I'm seeing different behaviour compared to if I run it from the terminal and I don't understand why.
I'm trying to do something a little bit out there, in that to save people typing pnpm {scriptName} --filter {packageName} I created a little script that they could run instead. It basically takes scriptName and lists all the available packages they can select from, and then trigger spawns a new process calling that command. Something like:
spawn("pnpm", ["--filter " + packageName, scriptName], { stdio: "inherit" });
Example:
In my particular case, I'm trying to test a script that ends up generating a pnpm --filter #ig/main test:debug.
I'm struggling a little though, in that if I invoke that via a terminal it works fine (test:debug is defined both in .\package.json and .\apps\main\package.json). However if I invoke it via the spawn command in node, then for some reason it invoking the test:debug script in the root, rather than just in apps/main. Does anyone know what that might be the case?
Turns out this was down to the current working directory not being set correctly when using spawn.

How can I run a node.js server

I'm new to node and have many things unclear.
Like, for php, I just need a index.php file on server's root dir and it can work by itself.
However, for a node.js file, we need to "node" command it in terminal right?
So what if we close that terminal? How can I keep it running to accept my requests?
You are correct in saying that the 'node' command will start a node process with whatever script you supply to it.
As far as keeping it running, there are several ways to do it. There are plenty of CLI libraries that will help you. For example, this one is called Forever
If you are using linux, you can simply run the node process as a background task:
node server.js &
To run node without terminal, you might want to check out one of these modules depending on your platform:
node-mac
node-windows
node-linux

node.js command prompt not initialised as expected

I installed the .msi file from nodejs website and installed it.
Now, when I run nodejs.exe, I do get a command prompt, but it shows a blinking > by default, instead of C:/>
It looks somewhat like this:
What to do?
This is called the REPL. You can enter statements for Node to execute and get realtime feedback. Ctrl+C twice will get you back to the command prompt.
I recommend checking out the answers to How do I get started with Node.js for learning more about Node.js and how it works. Typically you provide a file with your Javascript for Node to execute:
node app.js
or you can leave the .js off:
node app

Node: Move the current directory of the interactive shell

I'm writing a little CLI tool for Node.js. There are some situations where I'd like to run modify the current directory of the shell that runs my program:
some/location$ myProg moveToSomewhere
some/other/location$
child_process can't help here, since its commands run in a different shell.
Is there a way to do this?

Run a node.js server from Geany

A simple question: Is it possible to configure the Geany IDE so that Node.js servers can be run directly from Geany using the "Run" button?
When inside a JS file, go to Build > Set Build Commands, there should be a section title Execute commands. To use node to execute your files, put: node "%f" in the "Execute" command textbox.
When you change this, any .js files you are editing will run node in the virtual terminal when you hit F5.
If you want to set up an entire project to run the server whenever you're working somewhere within a given directory structure, you'll have to mess with project-level configuration. (something I don't usually bother with) My solution here just gives you a quick way to execute a single JS file without using an external terminal.
UPDATE: node "%f" seems to be legacy, but nodejs "%f" works

Resources