Last modified: Jun 30, 2025 By Alexander Williams

Fix Error: Cannot find module '../../someFile'

Node.js developers often encounter the "Cannot find module" error. This happens when Node.js cannot locate the file you are trying to import. Let's explore how to fix it.

Understanding the Error

The error occurs when you use require() or import to load a module that Node.js cannot find. The message usually looks like this:


Error: Cannot find module '../../someFile'

Common Causes

1. Incorrect File Path

The most common reason is a wrong path. Double-check your file path for typos or incorrect directory levels.

2. Missing File

The file you're trying to import might not exist. Verify the file exists in the specified location.

3. Case Sensitivity

File paths are case-sensitive on Unix systems. Ensure the case matches exactly.

How to Fix It

1. Verify the File Path

Check if the path is correct. Use absolute paths if relative paths are confusing. Example:


// Instead of this
const someFile = require('../../someFile');

// Try this
const path = require('path');
const someFile = require(path.join(__dirname, '..', '..', 'someFile'));

2. Check File Existence

Use the fs.existsSync() method to verify the file exists:


const fs = require('fs');
const path = './someFile.js';

if (fs.existsSync(path)) {
  console.log('File exists');
} else {
  console.log('File not found');
}

3. Check File Extensions

Node.js might not find files without proper extensions. Either:

- Add the extension to your require statement

- Or name your file with .js extension


// Add extension
const someFile = require('../../someFile.js');

4. Reinstall Dependencies

If it's a node module (like in our related article Fix Error: Cannot find module 'pg'), try reinstalling:


npm install

Advanced Solutions

1. Use path.resolve()

For complex path structures, path.resolve() can help create absolute paths:


const path = require('path');
const absolutePath = path.resolve(__dirname, '../../someFile.js');

2. Check NODE_PATH

If you're using NODE_PATH environment variable, ensure it's set correctly. This is similar to the solution in our Fix Error: Cannot find module 'typescript' article.

Prevention Tips

1. Use Consistent Import Styles

Stick to either relative or absolute paths throughout your project.

2. Add TypeScript Paths

If using TypeScript, configure paths in tsconfig.json like in Fix Error: Cannot find module '@types/node'.

3. Use Linters

ESLint can catch invalid import paths before runtime.

Conclusion

The "Cannot find module" error is common but easy to fix. Always verify your file paths and existence first. For node modules, reinstall dependencies. Use absolute paths when relative paths become confusing.

Remember that similar solutions apply to other module errors like those covered in our related articles. With these tips, you should be able to resolve path-related issues quickly.