unicorn/prefer-import-meta-properties Pedantic
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:
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:
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:
{
"rules": {
"unicorn/prefer-import-meta-properties": "error"
}
}import { defineConfig } from "oxlint";
export default defineConfig({
rules: {
"unicorn/prefer-import-meta-properties": "error",
},
});oxlint --deny unicorn/prefer-import-meta-propertiesVersion
This rule was added in v1.59.0.
