Skip to content

Latest commit

 

History

History
41 lines (28 loc) · 1.44 KB

prefer-import-meta-properties.md

File metadata and controls

41 lines (28 loc) · 1.44 KB

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

🚫 This rule is disabled in the ✅ recommended config.

🔧 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;