import/newline-after-import

Require an empty line between the last import and the code that follows

fixable

Also called import-x/newline-after-import

An empty line after the imports shows where the dependency list ends and the file's code begins.

The rule checks the last import in each run of imports, and does the same for top-level require() calls. A require() inside a function or a block isn't checked.

--fix adds the missing empty lines.

Configure#

Off by default. To turn it on:

bunfig.toml
[lint.rules]
"import/newline-after-import" = "error" # or "warn"

Every option, set to its default:

bunfig.toml
[lint.rules]
"import/newline-after-import" = ["error", {
  count = 1,
  exactCount = false,
  considerComments = false,
}]

Examples#

example.js1 problem
import { readFile } from "node:fs/promises";
import { join } from "node:path";
const root = join(import.meta.dirname, "..");
example.js1 problem
const { join } = require("node:path");
const root = join(__dirname, "..");
example.jsNo problems
import { readFile } from "node:fs/promises";
import { join } from "node:path";

const root = join(import.meta.dirname, "..");
example.jsNo problems
const { join } = require("node:path");

const root = join(__dirname, "..");

function loadPlugin(name) {
  const plugin = require("./plugins.js");
  return plugin[name];
}

Options#

OptionDefaultWhat it does
count1The minimum number of empty lines.
exactCountfalseAlso report more than count empty lines. --fix doesn't remove them.
considerCommentsfalseRequire the empty lines before a comment that follows the imports. By default a comment can come right after them, and only the distance to the next statement counts.
example.jswith { "count": 2 }1 problem
import { join } from "node:path";

const root = join(import.meta.dirname, "..");
example.jsNo problems
import { join } from "node:path";



const root = join(import.meta.dirname, "..");
example.jswith { "exactCount": true }1 problem
import { join } from "node:path";



const root = join(import.meta.dirname, "..");
example.jsNo problems
import { join } from "node:path";
// The root of the repository.

const root = join(import.meta.dirname, "..");
example.jswith { "considerComments": true }1 problem
import { join } from "node:path";
// The root of the repository.

const root = join(import.meta.dirname, "..");