home PYTHONJAVA
 

Node.js Errors

The Node.js applications generally face four types of errors:

  • Standard JavaScript errors i.e. <EvalError>, <SyntaxError>, <RangeError>, <ReferenceError>, <TypeError>, <URIError> etc.
  • System errors
  • User-specified errors
  • Assertion errors

Node.js Errors Example 1

Let's take an example to deploy standard JavaScript error - ReferenceError.

File: error_example1.js

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

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

  1. node error_example1.js  

Node.js Errors Example 2

File: timer2.js

  1. const fs = require('fs');  
  2. function nodeStyleCallback(err, data) {  
  3.  if (err) {  
  4.    console.error('There was an error', err);  
  5.    return;  
  6.  }  
  7.  console.log(data);  
  8. }  
  9. fs.readFile('/some/file/that/does-not-exist', nodeStyleCallback);  
  10. fs.readFile('/some/file/that/does-exist', nodeStyleCallback);  

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

  1. node error_example2.js  





welookups is optimized for learning.© welookups. 2018 - 2019 All Right Reserved and you agree to have read and accepted our term and condition.