Skip to main content
interlace
Plugin: import-nextRules

no-relative-packages

Forbid importing packages through relative paths

💼 This rule is enabled in the following configs: recommended, typescript. 🔧 This rule is automatically fixable by the --fix CLI option.

Rule Details

In a workspace, a relative specifier that climbs out of the importing file's package into another package bypasses that package's name, its exports map and its build output. This rule reports such a specifier and fixes it to the package name (../../other/src/x becomes other/src/x, ../../other becomes other).

A package is a directory whose package.json declares a name. The rule finds the nearest one above the importing file and above the import target, and reports when they differ. Not reported:

  • an import inside one package, including across a nameless package.json such as a {"type":"module"} scope marker in a sub-folder;
  • a target whose nearest package.json has no name (or cannot be read): there is no package name to import it by;
  • a target in a package that contains the importing one (the workspace root);
  • package specifiers and absolute paths.

Every static spelling is checked: import, import type, side-effect imports, export { a } from, export * from, import(), require() and TS import x = require().

// ❌ Incorrect (in packages/app/src/app.ts)
import { x } from '../../other/src/x';
export * from '../../other';

// ✅ Correct
import { x } from 'other/src/x';
export * from 'other';
import { util } from './util';

allowSamePackage is accepted for configuration compatibility and changes nothing: an import inside one package is never reported.

Options

[
  {
    "type": "object",
    "properties": {
      "allowSamePackage": {
        "type": "boolean",
        "default": true
      }
    },
    "additionalProperties": false
  }
]

Implementation

OWASP Foundation

  • Category: A00:2021 - General Security

Use this rule

npm install -D oxlint-plugin-import-next
.oxlintrc.json
{  "jsPlugins": ["oxlint-plugin-import-next"],  "rules": {    "import-next/no-relative-packages": "error"  }}
vite.config.ts
import { defineConfig } from 'vite-plus';export default defineConfig({  lint: {    jsPlugins: ['oxlint-plugin-import-next'],    rules: {      'import-next/no-relative-packages': 'error',    },  },});

oxlint-plugin-import-next re-exports eslint-plugin-import-next/oxlint: the same rules under the same IDs, and no ESLint install. Why oxlint.

Did this rule catch something? Star the repo to get new CWE coverage as we ship it — or follow the AI-code-security benchmarks behind these rules.