npm.io
1.0.1 • Published 2 months ago

relative-import-path

Licence
MIT
Version
1.0.1
Deps
0
Size
11 kB
Vulns
0
Weekly
0
Stars
1


relative-import-path
Styled with prettier Build status Code Coverage NPM version Downloads

Like path.relative, but for generating short require'able paths.

  • Removes unnecessary node_modules from resolved relative paths.
  • Keeps directories which cannot be a package name (eg pnpm's .pnpm virtual store) as a relative path, since they hold no importable module.
  • Automatically converts windows style paths to POSIX.

Installation

npm install relative-import-path

API

/**
 * Given two absolute file paths, resolves a short require'able relative path.
 */
function resolveRelativePath(from: string, to: string): string;

Examples

import { relativeImportPath } from "relative-import-path";

relativeImportPath("/a/b", "/c/d"); // /c/d
relativeImportPath("/a/a", "/a/b"); // ./b
relativeImportPath("/a/a/a", "/a/b/a"); // ../b/a
relativeImportPath("/a/node_modules/a/a", "/a/node_modules/b/a"); // b/a
relativeImportPath("/a/node_modules/a/a", "/a/node_modules/a/b"); // ./b
relativeImportPath("/a/a", "/node_modules/b"); // b
relativeImportPath("/a/a", "/a/node_modules/b"); // b
relativeImportPath("/a/b/c", "/a/node_modules/b"); // b
relativeImportPath("/a/a", "/b/node_modules/b"); // /b/node_modules/b
relativeImportPath("/a/b/c", "/b/node_modules/b"); // /b/node_modules/b
relativeImportPath("/a/a", "/a/b/node_modules/b"); // ./b/node_modules/b
relativeImportPath("/a/node_modules/@a/a/a", "/a/node_modules/@a/b/a"); // @a/b/a
relativeImportPath("/a/node_modules/@a/a/a", "/a/node_modules/@a/a/b"); // ./b
relativeImportPath("/a/a", "/a/node_modules/b/node_modules/c/a"); // b/node_modules/c/a
relativeImportPath("/a/a", "/a/node_modules/.pnpm/b@1.0.0/node_modules/b/a"); // ./node_modules/.pnpm/b@1.0.0/node_modules/b/a

Code of Conduct

This project adheres to the eBay Code of Conduct. By participating in this project you agree to abide by its terms.

Keywords