I have a Node server which I would like to debug. Once I've started the server
node server.js
I want to execute functions defined in server.js from a command line. The usual Node REPL "blocks" after the server has started.
For example, if server.js defines the function addBlogPost I want to locally call addBlogPost() and observe changes in the database without passing through a GUI.
Is there an easy way to do this?
You can use the repl module to create a new REPL instance:
repl = require("repl")
r = repl.start("node> ")
r.context.pause = pauseHTTP;
r.context.resume = resumeHTTP;
Now inside the REPL you can use pause() to call pauseHTTP() and resume() to call resumeHTTP().
Related
When I use the NodeJS repl on linux, everything I type is echoed twice. If I start it up like this:
$ node
Welcome to Node.js v16.11.0.
Type ".help" for more information.
> let repl;
undefined
> import("repl").then(module => { repl = module });
Promise {
<pending>,
[Symbol(async_id_symbol)]: 294,
[Symbol(trigger_async_id_symbol)]: 283,
[Symbol(destroyed)]: { destroyed: false }
}
> let f = repl.start();
> undefined
>
Now everything that I type is doubled up. If I type the letter d, it shows dd. This is preventing me from creating my own REPL setup script.
What am I doing wrong here?
When you start node.js in a terminal without passing a javascript file, by default node actually creates a REPL instance behind the scenes to give you something to interact with in your terminal. So by importing and starting another REPL you now have two REPL instances reading stdin and so you get twice the echo of stdin to stdout.
You can access the default running REPL by running this.repl in a newly opened node terminal. If you want only your own REPL to run, I would recommend declaring your REPL in a javascript file and then executing that file with the node runtime instead.
I have below piece of code in test.js file
function foo(){
console.log(this.bar);
}
var bar = "bar1";
var obj = {bar: "bar2"};
foo();
foo.call(obj);
when I run by 'node test.js', I got result
undefined
bar2
when I run in node .editor, I got result
bar1
bar2
I think the second result is right, but what is wrong with the first way? cuz I always do the first way.
what are the differences between them?
In simple terms when you run the your code using node test.js, Node JS will require('test.js') to run it. But when you are using the REPL mode, code you enter will execute directly in the REPL.
When using require, the variables on your test.js won't bind as global variables. So cannot access like this.bar
I think when using 'node test.js', node create a wrapper for test.js code, so there is no global variable bar.
while in .edit way, there is a variable bar defined in global scope.
I just started using electron. I have a doubt about how to pass command line arguments in electron when I'm using npm start to run electron.
In Node.js I am using: node server.js one two=three four
command prompt for :
var arguments = process.argv.slice(2);
arguments.forEach(function(val,index, array) {
console.log(index + ': ' + val);
});
In Node.js is working. I need to know how can I make this work in electron.
Can someone please give a solution for this?
The way of passing arguments will be same, the only thing you have to take care is path of electron. In package.json its written npm start will perform electron main.js. So you will have to execute this command explicitly and pass arguments with "proper path of electron" i.e ./node_modules/.bin/electron. Then the command will be
./node_modules/.bin/electron main.js argv1 argv2
and these arguments you can access by process.argv in main.js
and If wish you to access these parameters in your app then there are following things to do :
1.In your main.js define a variable like
global.sharedObject = {prop1: process.argv};
2.In your app just include remote and use this sharedObject
const remote = require('electron').remote;
const arguments = remote.getGlobal('sharedObject').prop1;
console.log(arguments);
3.Output will be ["argv1", "argv2"]
I have connected node inspector to my running node.js program. But I'd like to issue commands through the console - not necessarily on a breakpoint. For example, I have global variables and functions in my server.js program. For example:
var express = require('express');
var app = express();
function test() {
console.log('yo');
}
app.listen(3000);
Now in node-inspector I go into the console and I type 'test()' and it returns "ReferenceError: test is not defined", same things with global variables I type 'app' and it tells me it's not defined. Any idea how to make this thing work? I just want to run my node program and then issue commands to it via a command line interface.
#foreyez,
Your question inspired me to make an npm module that lets you debug (via command line) a running node.js application without setting breakpoints or needing to make variables global:
https://github.com/go-oleg/debug-live
Hope that helps!
The reason why it doesn't work as you expected is that all your variables and functions are local to your module (application) and you can't access them from a global context.
You can save them in the global scope if you want to access them from anywhere (including from the console when not stopped on a breakpoint):
var express = require('express');
var app = express();
function test() {
console.log('yo');
}
app.listen(3000);
global.test = test;
I am trying to debug the child_process example from here using IntelliJ IDEA 12.1.3 and node 10.10. When I run nodejs app.js from a terminal everything works. The console output displays as expected. However, when I debug the same script using IDEA there are no messages in console output and the app just sits there. This is what is in the console window:
/usr/bin/nodejs --debug-brk=58954 app.js
debugger listening on port 58954
debugger listening on port 58954
When I run the script in IDEA without the debugger attached, the script works as expected.
Why does attaching the debugger break the script?
You can force the children to use a free port for debugging. InteliJ will automatically pick up the port chosen by the child process.
Here's an example:
// Determine if in debug mode.
// If so, pass in a debug-brk option manually, without specifying port.
var startOpts = {};
var isInDebugMode = typeof v8debug === 'object';
if(isInDebugMode) {
startOpts = {execArgv: ['--debug-brk']};
}
child_process.fork('./some_module.js', startArgs, startOpts);
looks like a bug in node.js fork to me: both parent and child processes receive --debug-brk=58954 switch and attempt to start debugger and listen port 58954.