# Fix '__dirname is not defined in ES module scope'

> Learn how to fix the __dirname is not defined in ES module scope error by recreating it with fileURLToPath(import.meta.url) and path.dirname in Node.js.

Author: [Flavio Copes](https://flaviocopes.com/about/) | Published: 2022-04-18 | Topics: [Node.js](https://flaviocopes.com/tags/node/) | Canonical: https://flaviocopes.com/fix-dirname-not-defined-es-module-scope/

I stumbled on this error while I used `__dirname` inside a [ES module](https://flaviocopes.com/es-modules/).

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](https://flaviocopes.com/javascript/) file resides**, and many [Node.js](https://flaviocopes.com/nodejs/) 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](https://flaviocopes.com/github/) issues.

You first need to import the [Node.js `path` module](https://flaviocopes.com/node-module-path/) and the `fileURLToPath` function from the `url` module:

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

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

```js
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:

```js
console.log(__dirname)
```
