prefer-import-meta-properties

March 27, 2026 ยท View on GitHub

๐Ÿ“ Prefer import.meta.{dirname,filename} over legacy techniques for getting file paths.

๐Ÿšซ This rule is disabled in the following configs: โœ… recommended, โ˜‘๏ธ unopinionated.

๐Ÿ”ง This rule is automatically fixable by the --fix CLI option.

Starting with Node.js 20.11, import.meta.dirname and import.meta.filename have been introduced in ES modules.

import.meta.filename is the same as the url.fileURLToPath() of the import.meta.url.
import.meta.dirname is the same as the path.dirname() of the import.meta.filename.

This rule replaces legacy patterns with import.meta.{dirname,filename}.

Examples

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

// โŒ
const filename = fileURLToPath(import.meta.url);

// โœ…
const filename = import.meta.filename;
import path from 'node:path';
import {fileURLToPath} from 'node:url';

// โŒ
const dirname = path.dirname(fileURLToPath(import.meta.url));
const dirname = path.dirname(import.meta.filename);
const dirname = fileURLToPath(new URL('.', import.meta.url));

// โœ…
const dirname = import.meta.dirname;