Path: blob/main/docs/rules/no-relative-parent-imports.md
829 views
import/no-relative-parent-imports
Use this rule to prevent imports to folders in relative parent paths.
This rule is useful for enforcing tree-like folder structures instead of complex graph-like folder structures. While this restriction might be a departure from Node's default resolution style, it can lead large, complex codebases to be easier to maintain. If you've ever had debates over "where to put files" this rule is for you.
To fix violations of this rule there are three general strategies. Given this example:
You can,
Move the file to be in a sibling folder (or higher) of the dependency.
three.js could be be in the same folder as add.js:
or since add doesn't have any imports, it could be in it's own directory (namespace):
Pass the dependency as an argument at runtime (dependency injection)
Make the dependency a package so it's globally available to all files in your project:
These are (respectively) static, dynamic & global solutions to graph-like dependency resolution.
Examples
Given the following folder structure:
And the .eslintrc file:
The following patterns are considered problems:
The following patterns are NOT considered problems: