Disable Node REPL pre-evaluation? - node.js

In the Node (v16.7.0) REPL, Node tries to evaluate my statement before I've finished typing it. For example, if I type 2 + 2, it displays a faint 4 on the next line before I hit the Enter key. Is there a way to disable this behavior? For most cases, it's not a problem; but when I'm performing expensive operations, the REPL lags or freezes up as I'm trying to finish typing my statement. To be clear, the problem is not that the interpreter is printing the output; it's that it's trying to evaluate my statement before I finish typing it. Thanks in advance for your help!

The REPL library calls that a preview (search for preview in the link for more info). I'm not aware how to launch the default node REPL with command line options (or some other kind of configuration options) so that previews are not shown, but the REPL itself is a library you can use, with all the defaults (or whatever you prefer) and removing previews - for this check the REPL library options.
A basic example is creating this script:
#!/usr/bin/env node
const repl = require('repl');
repl.start({
preview: false
});
Make sure you also have it as executable (in Linux/Mac from the command line it would be with chmod +x name-of-repl-script from the terminal), and well, run that script instead of node directly.
Alternatively you can run the script with node name-of-repl-script (here, assuming node is the NodeJS REPL executable and that it's in the path).

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.

PyCharm: Executing the script in the console rather than 'run'

By default, all scripts in PyCharm seem to execute in a separate python interpreter called "Run", which, as far as I can make out, is pretty much independent of the IPython console running alongside. Now, to execute any snippet of the script after the whole thing has been run, I can copy-paste into the Run pane, but this is not ideal as it is not an actual python/ipython console. If I want to execute in the console, I will need to run the whole thing again inside the console (and not just a snippet) because the console doesn't seem to recognize/store any of the variables when it was run, which is tedious.
I've searched for a solution, but the closest I got was to enable "show command line afterwords" in the Run Configurations. This just seems to throw up an error on the lines of "file not found", which makes no sense. I'm running my script through SSH into a remote server, if that helps.
Go to run --> edit configurations...
towards the button you'll see a checkbox that says "run with python console"
make sure its checked
try to edit the run config, via run -> edit configuration.
And check the path of the working directory or the script directory.

Clear command history of NodeJS REPL Console

I am using NodeJS version 4.2.1
I want to know the command for clearing the NodeJS REPL console history completely so it doesn't show previously executed commands when Up or Down arrow keys are pressed.
Any suggestions ?
The answer is actually simple :)
On Windows (my version is 10):
go to user folder: cd %userprofile%
empty file named .node_repl_history (in my case with vim text-editor) OR you can simple run: echo. > .node_repl_history and it will have same effect.
Per the nodejs documentation which can be found at the following link:
nodejs repl
Environment Variable Options
The built-in repl (invoked by running
node or node -i) may be controlled via the following environment
variables:
NODE_REPL_HISTORY - When a valid path is given, persistent REPL
history will be saved to the specified file rather than
.node_repl_history in the user's home directory. Setting this value to
"" will disable persistent REPL history. NODE_REPL_HISTORY_SIZE -
defaults to 1000. Controls how many lines of history will be persisted
if history is available. Must be a positive number. NODE_REPL_MODE -
may be any of sloppy, strict, or magic. Defaults to magic, which will
automatically run "strict mode only" statements in strict mode.
Persistent History
By default, the REPL will persist history between
node REPL sessions by saving to a .node_repl_history file in the
user's home directory. This can be disabled by setting the environment
variable NODE_REPL_HISTORY="".
I think you might be looking for console.clear(). At least as of v8.3.0 https://doc.codingdict.com/nodejs-ref/console.html#console_console_clear :)

node.js attach REPL to script

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

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

Resources