src/content/docs/linter/rules/no-global-dirname-filename.mdx
import { Tabs, TabItem } from '@astrojs/starlight/components';
<Tabs> <TabItem label="JavaScript (and super languages)" icon="seti:javascript"> ## Summary - Rule available since: `v2.0.0` - Diagnostic Category: [`lint/correctness/noGlobalDirnameFilename`](/reference/diagnostics#diagnostic-category) - This rule isn't recommended, so you need to enable it. - This rule has a [**safe**](/linter/#safe-fixes) fix. - The default severity of this rule is [**information**](/reference/diagnostics#information). - Sources: - Inspired from [`unicorn/prefer-module`](https://github.com/sindresorhus/eslint-plugin-unicorn/blob/main/docs/rules/prefer-module.md){
"linter": {
"rules": {
"correctness": {
"noGlobalDirnameFilename": "error"
}
}
}
}
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;
const filename = __filename;
const foo = { __filename }
if (__dirname.startsWith("/project/src/")) {}
const dirname = import.meta.dirname
const filename = import.meta.filename
const foo = {__filename: import.meta.filename };
if (import.meta.dirname.startsWith("/project/src/")) {}