Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save alex-kinokon/f8f373e1a6bb01aa654d9085f2cff834 to your computer and use it in GitHub Desktop.
Save alex-kinokon/f8f373e1a6bb01aa654d9085f2cff834 to your computer and use it in GitHub Desktop.
eslint-import-require-node-prefix
// License: https://unlicense.org/
// @ts-check
const { builtinModules } = require("module");
/**
* @type {import("eslint").Rule.RuleModule}
*/
module.exports = {
meta: {
type: "problem",
docs: {
description:
"Disallow imports of built-in Node.js modules without the `node:` prefix",
category: "Best Practices",
recommended: true,
},
fixable: "code",
schema: [],
},
create: context => ({
ImportDeclaration(node) {
const { source } = node;
if (source?.type === "Literal" && typeof source.value === "string") {
const moduleName = source.value;
if (builtinModules.includes(moduleName) && !moduleName.startsWith("node:")) {
context.report({
node: source,
message: `Import of built-in Node.js module "${moduleName}" must use the "node:" prefix.`,
fix: fixer => fixer.replaceText(source, `"node:${moduleName}"`),
});
}
}
},
}),
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment