Skip to content
← Back to rules

import/newline-after-import Style

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

What it does

Enforces having one or more empty lines after the last top-level import statement or require call.

Why is this bad?

Without a blank line, import/require declarations blend into the following logic, which hurts readability and makes changes harder to scan. A blank line clearly separates dependencies from implementation.

Examples

Examples of incorrect code for this rule:

js
import * as foo from "foo";
const FOO = "BAR";
js
import * as foo from "foo";
const FOO = "BAR";

import { bar } from "bar-lib";
js
const FOO = require("./foo");
const BAZ = 1;
const BAR = require("./bar");

Examples of correct code for this rule:

js
import defaultExport from "./foo";

const FOO = "BAR";
js
import defaultExport from "./foo";
import { bar } from "bar-lib";

const FOO = "BAR";
js
const FOO = require("./foo");
const BAR = require("./bar");

const BAZ = 1;

With count set to 2 this will be considered valid:

js
import defaultExport from "./foo";

const FOO = "BAR";
js
import defaultExport from "./foo";

const FOO = "BAR";

With count set to 2 these will be considered invalid:

js
import defaultExport from "./foo";
const FOO = "BAR";
js
import defaultExport from "./foo";

const FOO = "BAR";

With count set to 2 and exactCount set to true this will be considered valid:

js
import defaultExport from "./foo";

const FOO = "BAR";

With count set to 2 and exactCount set to true these will be considered invalid:

js
import defaultExport from "./foo";
const FOO = "BAR";
js
import defaultExport from "./foo";

const FOO = "BAR";
js
import defaultExport from "./foo";

const FOO = "BAR";
js
import defaultExport from "./foo";

const FOO = "BAR";

With considerComments set to false this will be considered valid:

js
import defaultExport from "./foo";
// some comment here.
const FOO = "BAR";

With considerComments set to true this will be considered valid:

js
import defaultExport from "./foo";

// some comment here.
const FOO = "BAR";

With considerComments set to true this will be considered invalid:

js
import defaultExport from "./foo";
// some comment here.
const FOO = "BAR";

Example options usage

json
{
  "rules": {
    "import/newline-after-import": ["error", { "count": 1 }]
  }
}

Configuration

This rule accepts a configuration object with the following properties:

considerComments

type: boolean

default: false

count

type: integer

default: 1

exactCount

type: boolean

default: false

How to use

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

json
{
  "plugins": ["import"],
  "rules": {
    "import/newline-after-import": "error"
  }
}
ts
import { defineConfig } from "oxlint";

export default defineConfig({
  plugins: ["import"],
  rules: {
    "import/newline-after-import": "error",
  },
});
bash
oxlint --deny import/newline-after-import --import-plugin

Version

This rule was added in vnext.

References