Home >>Nodejs Tutorial >Node.js Errors

Node.js Errors

Node.js Errors

In general, the Node.js applications face four types of errors:

Standard JavaScript errorss i.e. <EvalError>, <RangeError>, <SyntaxError>, <ReferenceError>, <URIError>>, <TypeError>,etc.

User-specified errors

Assertion errors

Node.js Errors Example 1

Let's take an example of deploying standard error in JavaScript-ReferenceError.


// Throws with a ReferenceError because b is undefined  
try {  
const a = 1;  
const c = a + b;  
} catch (err) {  
console.log(err);  
}  

Open the command prompt Node.js and run the code below:

node error_example1.js 

Node.js error

Node.js Errors Example 2

File: timer2.js


const fs = require('fs');  
function nodeStyleCallback(err, data) {  
 if (err) {  
   console.error('There was an error', err);  
   return;  
 }  
 console.log(data);  
}  
fs.readFile('/some/file/that/does-not-exist', nodeStyleCallback);  
fs.readFile('/some/file/that/does-exist', nodeStyleCallback);  

Open the command prompt Node.js and run the code below:

node error_example2.js

Node.js error