How to fix “__dirname is not defined in ES module scope”

I stumbled on this error while I used __dirname inside a ES module.

In an ES module, you cannot use __dirname.

Using __dirname in a Node script you can get the path of the folder where the current JavaScript file resides, and many Node.js projects use this.

But if you use it inside an ES module, you can’t use this, as the infamous “__dirname is not defined in ES module scope” error shows up.

What can you do in this case?

I solved this problem by using a solution I found on the Node.js GitHub issues.

You first need to import the Node.js path module and the fileURLToPath function from the url module:

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

Then you can replicate the functionality of __dirname in this way:

const __filename = fileURLToPath(import.meta.url);

const __dirname = path.dirname(__filename);

This, incidentally, also replicates __filename, which returns the filename of the code which is executed.

Now you can use __dirname as usual:

console.log(__dirname)


This content originally appeared on flaviocopes.com and was authored by flaviocopes.com

I stumbled on this error while I used __dirname inside a ES module.

In an ES module, you cannot use __dirname.

Using __dirname in a Node script you can get the path of the folder where the current JavaScript file resides, and many Node.js projects use this.

But if you use it inside an ES module, you can’t use this, as the infamous “__dirname is not defined in ES module scope” error shows up.

What can you do in this case?

I solved this problem by using a solution I found on the Node.js GitHub issues.

You first need to import the Node.js path module and the fileURLToPath function from the url module:

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

Then you can replicate the functionality of __dirname in this way:

const __filename = fileURLToPath(import.meta.url);

const __dirname = path.dirname(__filename);

This, incidentally, also replicates __filename, which returns the filename of the code which is executed.

Now you can use __dirname as usual:

console.log(__dirname)


This content originally appeared on flaviocopes.com and was authored by flaviocopes.com


Print Share Comment Cite Upload Translate Updates
APA

flaviocopes.com | Sciencx (2022-04-18T05:00:00+00:00) How to fix “__dirname is not defined in ES module scope”. Retrieved from https://www.scien.cx/2022/04/18/how-to-fix-__dirname-is-not-defined-in-es-module-scope/

MLA
" » How to fix “__dirname is not defined in ES module scope”." flaviocopes.com | Sciencx - Monday April 18, 2022, https://www.scien.cx/2022/04/18/how-to-fix-__dirname-is-not-defined-in-es-module-scope/
HARVARD
flaviocopes.com | Sciencx Monday April 18, 2022 » How to fix “__dirname is not defined in ES module scope”., viewed ,<https://www.scien.cx/2022/04/18/how-to-fix-__dirname-is-not-defined-in-es-module-scope/>
VANCOUVER
flaviocopes.com | Sciencx - » How to fix “__dirname is not defined in ES module scope”. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/04/18/how-to-fix-__dirname-is-not-defined-in-es-module-scope/
CHICAGO
" » How to fix “__dirname is not defined in ES module scope”." flaviocopes.com | Sciencx - Accessed . https://www.scien.cx/2022/04/18/how-to-fix-__dirname-is-not-defined-in-es-module-scope/
IEEE
" » How to fix “__dirname is not defined in ES module scope”." flaviocopes.com | Sciencx [Online]. Available: https://www.scien.cx/2022/04/18/how-to-fix-__dirname-is-not-defined-in-es-module-scope/. [Accessed: ]
rf:citation
» How to fix “__dirname is not defined in ES module scope” | flaviocopes.com | Sciencx | https://www.scien.cx/2022/04/18/how-to-fix-__dirname-is-not-defined-in-es-module-scope/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.