Skip to content
← Back to rules

unicorn/prefer-import-meta-properties Pedantic

🛠️ An auto-fix is available for this rule.

What it does ​

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

Why is this bad? ​

Starting with Node.js 20.11, import.meta.dirname and import.meta.filename have been introduced in ES modules. import.meta.filename is equivalent to url.fileURLToPath(import.meta.url). import.meta.dirname is equivalent to path.dirname(import.meta.filename). This rule replaces legacy patterns with import.meta.dirname and import.meta.filename.

fileURLToPath(new URL('.', import.meta.url)) and the equivalent './' form are allowed because they return a path with a trailing separator, unlike import.meta.dirname.

Examples ​

Examples of incorrect code for this rule:

js
import path from "node:path";
import { fileURLToPath } from "url";

const filename = fileURLToPath(import.meta.url);
const dirname = path.dirname(fileURLToPath(import.meta.url));
const dirname = path.dirname(import.meta.filename);

Examples of correct code for this rule:

js
const filename = import.meta.filename;
const dirname = import.meta.dirname;

How to use ​

To enable this rule using the config file or in the CLI, you can use:

json
{
  "rules": {
    "unicorn/prefer-import-meta-properties": "error"
  }
}
ts
import { defineConfig } from "oxlint";

export default defineConfig({
  rules: {
    "unicorn/prefer-import-meta-properties": "error",
  },
});
bash
oxlint --deny unicorn/prefer-import-meta-properties

Version ​

This rule was added in v1.59.0.

References ​