124

I have been using Node version 12.3.4 updated it to 14.14.0 and started to receive a lot of issues which I fixed. The only thing that I don't understand is the issue

__dirname is not defined

__dirname is a core variable in Node as I know, Is it removed in Node 14?

Eduard
  • 1,768
  • 2
  • 10
  • 14
  • 4
    Is this in MJS or CJS? In MJS mode you need [a shim](https://stackoverflow.com/questions/46745014/alternative-for-dirname-in-node-when-using-the-experimental-modules-flag) since it works differently with `import`. – tadman Oct 16 '20 at 06:23
  • 2
    [documentation](https://nodejs.org/api/esm.html#esm_no_require_exports_module_exports_filename_dirname) – Jaromanda X Oct 16 '20 at 06:27

5 Answers5

216

How are you loading the file? According to this issue, the problem arises if you load it as an ECMAScript module which do not contain __dirname.

https://github.com/nodejs/help/issues/2907#issuecomment-671782092

Following the documentation below you may be able to resolve the issue: https://nodejs.org/api/esm.html#esm_no_require_exports_module_exports_filename_dirname

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

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
adlopez15
  • 3,449
  • 2
  • 14
  • 19
  • 2
    I was getting "dirname is not defined", so the last line I had to change to `const __dirname = path.dirname(__filename);` – 6bytes Feb 28 '22 at 22:22
  • This matches "official" advice on https://github.com/nodejs/help/issues/2907#issuecomment-757446568 – Beni Cherniavsky-Paskin Mar 28 '22 at 18:37
  • Thank you so much. I was stuck on this. I reformatting to es6 and my 'path' import broke. Code before was something like res.status(200).sendFile(path.join(__dirname, '/public/main.js')). I used the imports you recommended and changed to res.status(200).sendFile(__dirname + '/public/main.js'). Again, thank you, thank you. – Tim Sonner Sep 26 '22 at 11:26
  • `export default function fileAndDirNames(url) { const __filename = fileURLToPath(url); const __dirname = dirname(__filename); return {__filename, __dirname} }` needs to be a util function – some_groceries Jul 25 '23 at 02:15
105

My code before was like below.

app.use(express.static(path.join(__dirname, 'public')));

And I got this error.

ReferenceError: __dirname is not defined in ES module scope

And I solved this by adding code below.

import path from 'path';
const __dirname = path.resolve();
padaleiana
  • 955
  • 1
  • 14
  • 23
Jiyoon Hur
  • 1,214
  • 1
  • 5
  • 4
  • 28
    This only resolves the cwd of the node process, so e.g. `node myscript.mjs` will work but `node dir/myscript.mjs` will not. – Dan Sep 10 '21 at 22:13
  • I was actually trying to fix `path.resolve(__dirname, "foo", "bar")`, because I didn't realize that it will make the path relative anyway if you just pass it path segements -- you can simply omit the `__dirname`. Thanks! – Coderer Jul 18 '22 at 14:24
33

There's usually no need to import from 'url' or 'path'.

E.g. (using ESM)

fs.readFileSync(new URL('myfile.txt', import.meta.url));

will read myfile.txt from the directory of the JavaScript file (not from the current working directory).

ChrisV
  • 8,748
  • 3
  • 48
  • 38
  • 1
    Cool solution, but doesn't feel very clear to those not in the know and/or not working in a browser context where using URL seems a bit out of place. Perhaps you could expand on when this might be particularly appropriate? – Dan Sep 10 '21 at 22:03
  • 2
    Ok, looks like I just need to update my thinking. path.resolve() only returns cwd. Other methods are long winded. URL concept is baked into ES6 modules. It seems this is the standard approach now https://stackoverflow.com/a/66651120/4682556. Thanks for this answer! – Dan Sep 10 '21 at 22:11
  • 1
    This returns a `Buffer`, if you want a string use: `fs.readFileSync(new URL('myfile.txt', import.meta.url), 'utf-8');` – Michael Stramel Oct 06 '21 at 22:27
  • This is awesome, thank you! No extra requires, no hacks, future proof. – Tomáš Kafka Nov 19 '21 at 15:45
  • Upvoted in awe. This is _the_ proper way to handle `file:` URLs from `import.meta.url`. – Константин Ван Dec 10 '22 at 04:20
  • 1
    @Dan Yeah, a URL is getting more true to its name: _uniform resource locator_. It is not necessarily limited to a resource on the web. – Константин Ван Dec 10 '22 at 04:26
  • How to use this approach in combination with `express.static()`? – Melroy van den Berg Jan 02 '23 at 17:34
5

Got this from above link

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

const app = express();

//we need to change up how __dirname is used for ES6 purposes
const __dirname = path.dirname(fileURLToPath(import.meta.url));
//now please load my static html and css files for my express app, from my /dist directory
app.use(express.static(path.join(__dirname ,'dist')));

//works...
PUGAZHARASAN
  • 51
  • 1
  • 2
3

A quick fix (depending on your project) would be to ensure that "type": "module" does not exist in your package.json file

Nathanael
  • 954
  • 3
  • 19
  • 39