Running console commands through node inspector? - node.js

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;

Related

Using the environment variable crashes the nodejs app

The app runs on a shared server at namecheap. When I hardcode the env variable as a regular variable inside server.js like let MONGO_URL=blablabla the app works OK.
When i use process.env.MONGO_URL instead, it crashes. Any ideas why it is happening?
note: I have my env variable installed on node console of cpanel.
note2: The problem is not MONGO_URL, using process.env.PORT crashes the app too.
is there process.env.MONGO_URL in list of variable that you installed on node console of cpanel?
if it is not, put it on that list and try again
Solved.
Added to the server.js
if (process.env.NODE_ENV != 'production') {
require('dotenv').config();
}
And switched the app mode to "production". Now process.env works.

process.env variables are undefined even after export

I'm writing a Node.js express app and want to use environment variables for setting the port on which the server should run.
However, I can't seem to get process.env.PORT to read my PORT environment variable.
I've defined the PORT environment variable using export like so:
export PORT=1234 I've also added this line to the ~/.bash_profile file, but process.env.PORT remains undefined.
When I run echo $PORT in terminal, it displays the value (1234) correctly.
I'm running Node V0.12.7 and OSX El Capitan 10.11.1 and really can't find any extra clues on what might be causing this.
Thanks!
EDIT:
Here's the code executed before trying to assign process.env.port to the port variable
var app = require('../app');
var proxy = require("../proxy");
var http = require('http');
var nconf = require('nconf');
nconf.file(__dirname + "/appConf.json");
var appSettings = require('../appSettings');
var port = normalizePort(process.env.PORT || 8080);
There's a great npm package called dotenv, that will auto-load any environment variables from a file called .env in the same directory as your nodeJS application. This could give you a differentPORT variable project to project, instead of globally defining one PORT across all terminals.
As far as everything else, it sounds like PORT should be there (you even said it was properly echo-ing from command line). Did you run node out of the same terminal as you used echo? The only other issues I can think of are if you didn't restart the terminal you're running your server out of after you modified your ~/.bash_profile, or maybe a simple typo somewhere.
I would have posted this as a comment, but I don't have the score for it.
Seems like there is a function or another object named process, it could cover the NodeJS process object.

How to pass command line argument in electron

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"]

application.js wont start but debug works

I am new to node.js and I want to get webserver running but I got this problem where trying to debug application by running node ./bin/www works perfectly but trying to launch it by node app.js dosen't do anything.
When i type out node app.js in terminal blank line appears like its loading something and dissapears in few seccods without any error or starting application.
The problem is that app.js is exporting an app object, not starting a server. If you look at the code for bin/www you'll see that it loads the app object and uses it to start a server.
#!/usr/bin/env node
var debug = require('debug')('tmp');
var app = require('../app');
app.set('port', process.env.PORT || 3000);
var server = app.listen(app.get('port'), function() {
debug('Express server listening on port ' + server.address().port);
});
You need to either start the app using bin/www, or modify and move that code to the end of app.js if you don't want the separate start script.
If you want to see the debug messages, you need to add the DEBUG environment variable to your shell session. For development environments, you could do this automatically by placing it in your .bashrc file.
export DEBUG=express:*
Or if you do not want to do that, you can just do this:
DEBUG=express:* node ./bin/www
http://expressjs.com/guide.html#debugging-express

Node different require behaviour same app different servers

I have a project (express js) in which the main app.js has its declarations, i.e:
var express = require('express'),
routes = require('./routes'),
main = require('./routes/main'),
config = require('./config.js'),
...
So in a file like routes/main.js I could access the properties of config. I've also installed my express js on another server so all of a sudden a route in /routes/main.js tells me
ReferenceError: config is not defined
So if I redeclare it in there, now there isn't a null reference error but everything becomes undefined.
Both of the servers are running the script the same way (via forever). I am using a full path when starting scripts, but it does work fine on one server (centos) and not the other (ubuntu), but there aren't any other differences.

Resources