Error: Cannot find module

Node.js require absolute path fails: 3 common fixes

Programming & Dev Tools Beginner 👁 1 views 📅 Jul 15, 2026

This error hits when you use absolute paths with require(). Most times it's a typo or wrong path, but there's a trick with module resolution. I'll show you the real fixes.

1. You mistyped the path (most common cause)

I see this all the time. You copy an absolute path from somewhere else or type it manually, and there's a missing slash, extra dot, or OS path separator issue. Node.js is picky about that.

On Windows, absolute paths start with C:\Users\...\file.js. On Linux/Mac, it's /home/user/.../file.js. If you're mixing backslashes and forward slashes, Node.js on Windows can handle it, but on Linux it won't.

Fix it fast: Instead of typing the path by hand, use path.resolve() or path.join(). These functions give you the correct separator every time.

const path = require('path');
const myModule = require(path.join('/root/projects/app', 'utils', 'helper.js'));

Or use require(path.resolve(__dirname, 'utils', 'helper.js')) — that's bulletproof because __dirname always points to the current file's folder.

Real-world trigger: You moved a project folder from Windows to a Linux server, and the absolute path in your code still uses backslashes. Node.js on Linux sees C:\Users\... and says "file not found."

2. The file or directory doesn't exist at that path

Another classic. You think the file is there, but it's not. Could be a typo in the folder name, a case mismatch (Linux is case-sensitive), or the file was deleted/renamed.

Check this: Run ls -la /full/path/to/file.js on Linux/Mac or dir C:\full\path\to\file.js on Windows. If it's not there, that's your problem.

If the path is a directory (like require('/some/folder')), Node.js looks for an index.js or index.json inside. If neither exists, you get the error.

Simple fix: Use require('./relative/path') instead of absolute paths when possible. Relative paths are less error-prone. But if you really need absolute, double-check with fs.existsSync() before the require.

const fs = require('fs');
const target = '/home/user/project/config.js';
if (fs.existsSync(target)) {
  const config = require(target);
} else {
  console.error('File not found:', target);
}

Real-world trigger: You upgraded a module that changed its internal folder structure. Your absolute path pointed to an old location that no longer exists.

3. Node.js module resolution doesn't work with absolute paths like you think

This one trips up people new to Node. When you use require('/some/path'), Node treats it as a file path, not a module lookup. That's fine if the path points to a real file. But if you expect Node to search node_modules or the core modules when using absolute paths, it won't.

The rule: Absolute paths in require() are for local files only. If you want Node to search node_modules, use a relative path starting with ./ or ../, or just a module name like require('lodash').

If you're trying to load a module from a different project directory, the cleanest way is to use npm link or set up a workspace. Don't mess with absolute paths for that.

Fix it: Switch to relative paths for local files, or use require.resolve() to see what Node actually finds.

// Bad: require('/home/user/project/node_modules/lodash');
// Good: require('lodash');

Real-world trigger: You copied a require line from a tutorial that used an absolute path for a global module, but your module is installed locally in node_modules.

Quick reference table

Cause Fix Code snippet
Path typing error Use path.join() or path.resolve() require(path.join(directory, 'file.js'))
File doesn't exist Check with fs.existsSync() fs.existsSync('path') && require('path')
Wrong module resolution Use relative path or module name require('./file') or require('module')

Was this solution helpful?