MEAN Stack Application's console.log not working? - node.js

I am currently developing a MEAN Stack application.
Previously my node version is 0.12 and I was using grunt to run my application which uses nodemon (which uses server.js) to run the server and watch to monitor for changes.
it works fine all the way till i upgrade my node to v 0.4.
now when I use grunt, the console.log does not print to console at all and it only works if i use
node server.js
instead of
grunt
the image below shows the situation that i'm facing:
in an ideal situation, when running grunt, I should be able to see the message
App listening on port 8000
however after updating node, the console.log doesn't seem to be working for my grunt(?)/nodemon(?) anymore.
can anyone advise on this matter?
thank you.

You can use very useful logging module winston. Using this you can log all your errors in a text file with timestamp, Which will be very helpful for application support. You can use it's debug, error, info methods for the corresponding console.debug(), console.error(), console.info().
EX:
Var logger = require('winston')
logger.debug('Debug things here');
logger.info('put info to track');
logger.error('to track error with status code')

Related

Make a logger for Node Js

I have a project in Node Js, which executes the project on port 3000 and I access from ngrok with my browser to said localhost port, and it executes a server on port 3001 to make requests to a Maria database db. The project is done in react and the server with express.
I want to save the application logs (errors, warnings, etc.) in a log file so that I can see them whenever I want.
My intention was to use winston, and while I have no problem on the server side (3001), when I try to adapt it to the main project, I get an error that it cannot save files (the reason that appears is that it runs from the browser, and you can't create such a file because you don't have access to the project folders)
Can anyone give me some advice? Am I wrong to use winston, and should I use another?
Greetings and thanks
I've never used winston before and I couldn't find anything online about your error. In the past I've always just used node's fs module to create a log of errors and restarts.
const fs = require('fs')
Node's File System Documentation: https://nodejs.dev/learn/the-nodejs-fs-module
Short YouTube Tutorial: https://www.youtube.com/watch?v=U57kU311-nE

Why is my Node.js service stopping unexpectedly under GraalVM?

I have a fairly simple Node.js service that basically just fields a few HTTP requests. This runs fine via the GraalVM node command. However, when I use node --jvm --polyglot service.js My Node service dies shortly after starting. Nothing else in the code has changed.
What is interesting is that the following code seems to kill my Node.js service
const { MongoClient } = require("mongodb")
console.log("got MongoClient")
And when I run Graal Node without --jvm --polyglot everything works fine.
If I comment out the Mongo stuff, running with --jvm --polyglot, everything works fine.
What could possibly be going on where trying to run the MongoDB Node.js driver under GraalVM could be causing problems?
It may not be that it dies, but after starting my HTTP service
const server = app.listen(port, () => console.log(`Server running... test at http://${hostname}:${port}/ping`))
it no longer accepts HTTP requests. ???
The best approach would be to raise an issue on GraalVM's repos, probably on the Graal.js one: https://github.com/oracle/graaljs. It could be a bug.
You can also debug the process and maybe that will reveal additional details of what's happening: https://www.graalvm.org/tools/chrome-debugger/

How to get nodejs on client-side on cordova?

I am making a app with cordova. I want the coinmarketcap stats in it. I am using nodejs and it is working fine when I execute it in my command line. However I want to use it in my cordova application. This is the link to the npm package https://www.npmjs.com/package/node-coinmarketcap
When I try to run it in a browser I get the error message require not defined. I believe that error comes because nodesj is a server-side and browser is a client side ? Am I correct ?
My code
var CoinMarketCap = require("node-coinmarketcap");
var coinmarketcap = new CoinMarketCap();
coinmarketcap.multi(coins => {
console.log(coins.getTop(10)); // Prints information about top 10 cryptocurrencies
});
I expect that I can run my code in my browser or cordova application.
You are right, Node.js is a JavaScript run-time environment that executes JavaScript code outside of a browser. You have to find some client-side JS code or execute your node code on your server and send result to your Cordova app.

Why does Electron/React app freeze and without sending errors to the log?

I have an Electron/React app with a load screen. The vast majority of the time when I make a mistake the app will send errors to Node or the console and I can debug. But with certain mistakes the app will freeze on the load screen with no logging at all. For example, if I add
const t = 5;
const t = 5;
to src/renderer/app/actiontypes.js I do not get the usual "Uncaught SyntaxError" message and I have to read very carefully through code to figure out what's going wrong.
Here is how the app loads:
main.js
app.on('ready', async () => {
await installExtensions();
createLoadingScreen();
ipcMain.on('robot-load-finished', () => {
mainWindow.show();
...
index.js
function run() {
ipcRenderer.send('robot-load-finished');
...
loadRobotModels().then(run);
Does anyone why this is occurring? Thank you.
Fixed this issue by setting up a chromium remote debug configuration in Webstorm. If you have the same issue and you use Webstorm, hopefully this tutorial will help you too.
Two other options are to use VSCode or node-inspector. However, node-inspector is not compatible with the latest versions of Node and the whole module seems to be abandoned due to Node's new --inspect flag. The electron team is planning to add support for the --inspect flag, here is the ticket to watch.

Console.log in a production app using node.js, express and PM2

I'm using console.log for debugging some information in my web app using node.js, expressjs and PM2. All is working fine :)
I want to know if my web app can have performance problems in production if I use console.log this:
var myVariable = "Enter in this section of code";
console.log(myVariable);
I have read about console.log is synchronous and can affect to my performance... if this is real in this moment (I'm using Express 4), how can I remove these console.log in different environments like production?
I'm using PM2 (pm2 logs command) to watch the logs and pm2 flush to clean the logs.
Thanks.
You could use winston for logging , you could configure transport depending on your env , for example you could use Console and File tranports in development and just File in production. PD: pm2 takes data from std log(console)

Resources