Skip to content

noGlobalDirnameFilename

Disallow the use of __dirname and __filename in the global scope.

They are not available in ES modules. Starting with Node.js 20.11, import.meta.dirname and import.meta.filename have been introduced in ES modules, providing identical functionality to __dirname and __filename in CommonJS (CJS).

const dirname = __dirname;
code-block.js:1:17 lint/nursery/noGlobalDirnameFilename  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Don’t use __dirname.

> 1 │ const dirname = __dirname;
^^^^^^^^^
2 │

__dirname is not available in ES modules.

Safe fix: Replace __dirname with import.meta.dirname.

1 - const·dirname·=·__dirname;
1+ const·dirname·=·import.meta.dirname;
2 2

const filename = __filename;
code-block.js:1:18 lint/nursery/noGlobalDirnameFilename  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Don’t use __filename.

> 1 │ const filename = __filename;
^^^^^^^^^^
2 │

__filename is not available in ES modules.

Safe fix: Replace __filename with import.meta.filename.

1 - const·filename·=·__filename;
1+ const·filename·=·import.meta.filename;
2 2

const foo = { __filename }
code-block.js:1:15 lint/nursery/noGlobalDirnameFilename  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Don’t use __filename.

> 1 │ const foo = { __filename }
^^^^^^^^^^^
2 │

__filename is not available in ES modules.

Safe fix: Replace __filename with import.meta.filename.

1 │ const·foo·=·{·__filename:·import.meta.filename·}
++++++++++++++++++++++
if (__dirname.startsWith("/project/src/")) {}
code-block.js:1:5 lint/nursery/noGlobalDirnameFilename  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Don’t use __dirname.

> 1 │ if (__dirname.startsWith(“/project/src/”)) {}
^^^^^^^^^
2 │

__dirname is not available in ES modules.

Safe fix: Replace __dirname with import.meta.dirname.

1 - if·(__dirname.startsWith(/project/src/))·{}
1+ if·(import.meta.dirname.startsWith(/project/src/))·{}
2 2

const dirname = import.meta.dirname
const filename = import.meta.filename
const foo = {__filename: import.meta.filename };
if (import.meta.dirname.startsWith("/project/src/")) {}
biome.json
{
"linter": {
"rules": {
"nursery": {
"noGlobalDirnameFilename": "error"
}
}
}
}