[Solved] __dirname is not defined in Node 14 version

Hello Guys, How are you all? Hope You all Are Fine. Today I am updated my node to version 14 and Now I am facing following error: __dirname is not defined in Node 14 version. So Here I am Explain to you all the possible solutions here.

How __dirname is not defined in Node 14 version Error Occurs ?

I am updated my node to version 14 and Now I am facing following error.

``````__dirname is not defined
``````

How To Solve __dirname is not defined in Node 14 version Error ?

1. How To Solve __dirname is not defined in Node 14 version Error ?

To Solve __dirname is not defined in Node 14 version Error You Need to define __dirname in this way: const __dirname = dirname(__filename); Now, Your error must be solved.

2. __dirname is not defined in Node 14 version

To Solve __dirname is not defined in Node 14 version Error You Need to define __dirname in this way: const __dirname = dirname(__filename); Now, Your error must be solved.

Solution 1: define __dirname in This way

You Need to define __dirname in this way.

``````import { fileURLToPath } from 'url';
import { dirname } from 'path';

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);``````

Now, Your error must be solved.

Solution 2: Use this

Just define __dirname lby adding following code.

``````import path from 'path';
const __dirname = path.resolve();``````

Now, Your error will be solved.

Summary

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you?