DEV Community

Tuan Phan
Tuan Phan

Posted on • Updated on

Mastering Node.js Debugging: 5 Common Errors and Solutions

1. ModuleNotFoundError: Cannot find module 'xyz':

Error: This occurs when Node.js is unable to locate the specified module ('xyz') in your project. Double-check the module name, file path, and ensure that the module is installed as a dependency in your package.json. Also, verify the case sensitivity of the module name, as Node.js file systems are case-sensitive.

Check Module Name and Path:
Verify that the module name and path specified in your require or import statement are correct. Ensure that the case of the module name matches the actual file or module name, as file systems in some environments are case-sensitive.

Example (CommonJS):

const xyzModule = require('./xyz');  // Check the path and file extension
Enter fullscreen mode Exit fullscreen mode

Example (ES6 Modules):

import xyzModule from './xyz';  // Check the path and file extension
Enter fullscreen mode Exit fullscreen mode

Verify Module Installation:
If 'xyz' is an external module installed via npm, make sure it is listed as a dependency in your package.json file. You can install it using:

npm install xyz
Enter fullscreen mode Exit fullscreen mode

Ensure that the module is correctly installed and appears in the node_modules directory.

Check Node.js Version:
Some modules may be designed for a specific version of Node.js. Ensure that your Node.js version is compatible with the module you are trying to use.

File Extension:
If you are importing a local module, make sure to include the file extension if the module file has one.

Example:

const xyzModule = require('./xyz.js');  // Include the file extension
Enter fullscreen mode Exit fullscreen mode

Check for Typos:
Review your code carefully for any typos in the module name or file path. A simple typo can lead to the module not being found.

Restart Your Application:
After making changes to your code, restart your application or development server. Sometimes, changes may not take effect until the application is restarted.

Reinstall Dependencies:
If the module is installed locally in your project, try reinstalling your project's dependencies using:

npm install
Enter fullscreen mode Exit fullscreen mode

Path Resolution:
Ensure that the module you are trying to import is in a location that can be resolved based on the current working directory or specified paths. Consider using absolute paths or adjusting your project structure if needed.

2. SyntaxError: Unexpected token '=>' or 'import':

Error: Node.js versions prior to 14 do not fully support ECMAScript modules (ESM) syntax like arrow functions (=>) or import statements. This error may occur when using ESM features without enabling ESM support in your Node.js environment. Consider upgrading to a newer Node.js version that supports ESM or use CommonJS syntax for older versions.

Use of Arrow Functions (=>):
The arrow function syntax (=>) is part of ECMAScript 6 (ES6) and might not be supported in older environments. Ensure that your JavaScript environment supports ES6 features.

Example:

// ES6 Arrow Function
const myFunction = () => {
  // code here
};

Enter fullscreen mode Exit fullscreen mode

If you're running the code in an environment that doesn't support ES6, consider using traditional function syntax.

Example:

// Traditional Function
function myFunction() {
  // code here
}
Enter fullscreen mode Exit fullscreen mode

Use of Import Statements:
The import statement is also an ES6 feature for module imports. If you encounter this error, make sure you are in an environment that supports ES6 modules.

Example:

// ES6 Import Statement
import { someModule } from 'some-module';
Enter fullscreen mode Exit fullscreen mode

If your environment doesn't support ES6 modules, you may need to use a different approach, such as CommonJS syntax (Node.js) or another module system.

Example (Node.js/CommonJS):

// CommonJS Require
const someModule = require('some-module');
Enter fullscreen mode Exit fullscreen mode

3. TypeError: Cannot read property 'xyz' of undefined:

Error: This error often indicates that a property or method is being accessed on an undefined object. Check the code where the property is used and ensure that the object is properly initialized or defined before attempting to access its properties or methods.

4. UnhandledPromiseRejectionWarning: Unhandled promise rejection: (reason):

Error: This warning occurs when a Promise is rejected, but there is no .catch() or await to handle the rejection. Always handle promise rejections by using .catch() or try/catch blocks to prevent unhandled promise rejections, which can lead to unexpected behavior or crashes in your Node.js application.

5. EADDRINUSE: Address already in use:

Error: This error indicates that the specified port is already in use by another process. It commonly occurs when trying to start a Node.js server on a port that is already occupied. Identify the conflicting process or choose a different port for your Node.js application.

Determine which processes are currently using the port that your program is attempting to bind. You can use command-line tools to identify these processes.

On Unix-based systems (Linux, macOS):

lsof -i :<port_number>
Enter fullscreen mode Exit fullscreen mode

On Windows:

netstat -ano | findstr :<port_number>
Enter fullscreen mode Exit fullscreen mode

Once you identify the process using the port, you can terminate it using the appropriate command or by using a task manager.

On Unix-based systems:

kill <process_id>
Enter fullscreen mode Exit fullscreen mode

On Windows:

taskkill /F /PID <process_id>
Enter fullscreen mode Exit fullscreen mode

Top comments (0)