When I have an Error in my code, compiling with ts-node, the error does not appear in the console.
For exemple:let data = await fs.readFileSync(path);
I have this code where I use "fs" to read a file, using a path that the function receive as parameter. But when the path is from a file that does not exists nothing happends in the console. The only thing that happens is that the requisition never finish loading.
If I put a console.log like this in the code, the error appears saying that the file does not exists : console.log(fs.readFile(path, (data) => { }))
It doesn't make sense, since the errors are supposed to show in the console. (This happens with lot of type of commum errors that make it harder to finish the application)
here's the repository with all the code https://github.com/Macintosh-1984/Pokedex_Express
somebody help please
You are not capture the error on readFile
fs.readFile(path, 'utf8', function(err, data){
// you have err here
});
Related
there! I am developing app with Next js. and i need to run php script to get some value.
For executing php code I use '"exec-php' package from npm. this package requires enter php file path to js function. But when I put path as parameter to function and call this function, next js throws error - unhandledRejection: Error: File './script.php' not found.
execPhp('./script.php', (err, php, out) => {
php.test(obj, process.env.KEY, (err, result, output, printed) => {
signature = result;
});
});
If you have idea how to solve please answer. Thanks~
I have a huge script (3 modules x 2000 lines each one) which runs about 3000 times per day. Few times a week it freezes. Nodejs process is still running on Linux server, but nothing happens, it just stops working without any understandable reason. I've tried to write logs everywhere it was possible with try ... catch, but there is still no errors or warnings. It hangs in different places, different loops. There are also no errors in server logs.
Are there any other ways to debug such problem?
in your main file( the file which your start app) put these codes :
// to log all bugs that occured in synchronus code but not handlled anywhere (add below code on top of your main file)
process.on('uncaughtException', err => {
console.log('uncaughtException!! shutting down...');
console.log(err.name, err.message);
process.exit(1);
});
// to log all unhandledRejection in you APP (Async Code)
process.on('unhandledRejection', (err) => {
console.error(`Error Name : ${err.name}\nError Message : ${err.message}`);
console.error(`unhandledRejection!💥 Shutting Down...`);
process.exit(1); // 0 for success & 1 for uncaught exeptions
});
By these lines of codes, when any problem happen in your app you can observe errors wich you haven't handle on your code.
Update: I was able to find the error in my code. The problem was that I didn't close my opening parenthesis with closing ones in my console.log for my notes.js.
I started a course on node.js and I am learning how to call other functions created from files on my local system. I have the directory node-notes in which I have two javascript files, app.js and notes.js. I am calling my notes.js in my app.js. I made a simple console.log() call just to make sure its working right in my notes.js. But when I try to run my code using the command node app.js I am getting the error below.
Update: I am using Node 10.0.0
I have no idea what this means so can someone please explain what the error is and how I can fix it. Below I will post both codes for my app.js and notes.js
app.js
console.log("STARTING app.js")
// calling modules using require
// calling functions in fs module and putting in the
// variable fs
const fs = require('fs');
const os = require('os');
const notes = require('./notes.js');
var user = os.userInfo();
console.log(user);
fs.appendFile('greetings.txt', 'Hello World!' + user.username , function(err){
if(err){
console.log("There was an error")
}else{
console.log("It was successful")
}
});
notes.js
console.log("Calling notes.js")
In notes.js you are missing a " to end the string.
console.log("Calling notes.js) should be console.log("Calling notes.js")
Thus you get a SyntaxError: Invalid or unexpected token
Here is the command that is broken:
fs.writeFileSync('Metadata.json', metadataString);
console.log("Metadata written.");
I have placed a breakpoint and verified that metadataString is actually a string. The console logs Metadata written just fine and the file stays empty...
Not sure what else I can check... Thanks in advance.
fs.writeFileSync() throws an Error exception if it fails. Use a try/catch block:
try {
fs.writeFileSync('Metadata.json', metadataString);
} catch (err) {
console.log('Error writing Metadata.json:' + err.message)
}
I'm grunt beginner. But whenever I install any kind of grunt library to handle images it does not work.
grunt-webshot, webshot, getPixels, node-image, node, gm, node-pngjs, imagemagick, node-png, png, get-pixels
grunt.registerTask("getPixels", "your description", function() {
var fs = require('fs') , gm = require('gm');
gm('source/templates/t1/images/menu_item_bg.png').size(function (err, size) {
grunt.log.writeln(size.width > size.height ? 'wider' : 'taller than you');
});
});
Silently fails. No error, no nothing (I get: Done, without errors). Does not enter callback.
Same is with other libraries I used.
Only grunt modules can be used in grunt? Dont Think so, it seems i can use any library by using require() and it should work. It works ok with require('path')
Any way I can debug it? Make it return some kind of error or fix this problem?
Edit
I wrote a makescreenshot.js script which I call with node
var webshot = require('webshot');
// also tried commented varians:
//webshot('google.com', 'google.jpg', function(err) {
//webshot('http://google.com', 'google.jpg', function(err) {
webshot('http://google.com', 'google.jpg', {}, function(err) {
console.log('console.log ERROR: ' + err);
});
It returns the following: console.log ERROR: Error: PhantomJS exited with return value 1
Seems I figured it somehow. The problem seemed to be not properly installed Microsoft Visual Studio I reinstalled MSVS 2013 Express and it started taking screens normally.