Coding⏱️ 2 min read📅 2026-06-03

How to Fix: __dirname is not defined error in Node.js 14 version

Node.js version update issue

Quick Answer: The __dirname variable is not removed in Node.js 14, but its behavior changed due to the new ES module syntax. You need to use import.meta.url instead.

The __dirname is not defined error in Node.js 14 version can be resolved by updating the require paths to use absolute paths or relative paths with respect to the current working directory.

🛠️ Step-by-Step Verified Fixes

Method 1: Using Absolute Paths

  1. Step 1: Update the require path to use an absolute path, e.g., `require('./path/to/file.js')` or `require(__dirname + '/path/to/file.js').

Method 2: Using Relative Paths

  1. Step 1: Update the require path to use a relative path, e.g., `require('./path/to/file.js')` or `require('../path/to/file.js').

💡 Conclusion

By following these steps, you should be able to resolve the __dirname is not defined error in Node.js 14 version and get back to developing your application.

Did this fix your problem?

If not, try searching for specific error codes.

🔍 Search Error Database

❓ Frequently Asked Questions