# relative-import-path

> path.relative for generating short require'able paths.

Latest version **1.0.1** (published 2026-07-13) · MIT license · 0 weekly downloads

## Install

```sh
npm install relative-import-path
pnpm add relative-import-path
yarn add relative-import-path
bun add relative-import-path
```

## Health

**Score 70/100 (B)** — status: active.

Positive: has types; esm support; no vulnerabilities; has provenance; recently updated; high quality score.

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 1.0.1 |
| Published | 2026-07-13 |
| First published | 2022-03-11 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 11.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Provenance | attested (GitHub Actions) |
| GitHub stars | 1 |
| Author | Dylan Piercey |
| Maintainers | dylanpiercey |
| Keywords | node_modules, path, relative, require, resolve, shorten |

## Links

- npm: https://www.npmjs.com/package/relative-import-path
- Repository: https://github.com/marko-js/relative-import-path
- Issues: https://github.com/marko-js/relative-import-path/issues
- npm.io page: https://npm.io/package/relative-import-path

## Alternatives

- [base64url](https://npm.io/package/base64url.md) — 6.1M weekly downloads
- [get-installed-path](https://npm.io/package/get-installed-path.md) — 502.9K weekly downloads
- [@uppy/url](https://npm.io/package/@uppy/url.md) — 185.8K weekly downloads
- [@d3fc/d3fc-shape](https://npm.io/package/@d3fc/d3fc-shape.md) — 16.2K weekly downloads
- [localizer](https://npm.io/package/localizer.md) — 226 weekly downloads

## Recent versions

- 1.0.1 (latest) — 2026-07-13
- 1.0.0 — 2022-03-11

## README

<h1 align="center">
  <!-- Logo -->
  <br/>
  relative-import-path
	<br/>

  <!-- Format -->
  <a href="https://github.com/prettier/prettier">
    <img src="https://img.shields.io/badge/styled_with-prettier-ff69b4.svg" alt="Styled with prettier"/>
  </a>
  <!-- CI -->
  <a href="https://github.com/marko-js/relative-import-path/actions/workflows/ci.yml">
    <img src="https://github.com/marko-js/relative-import-path/actions/workflows/ci.yml/badge.svg" alt="Build status"/>
  </a>
  <!-- Coverage -->
  <a href="https://codecov.io/gh/marko-js/relative-import-path">
    <img src="https://codecov.io/gh/marko-js/relative-import-path/branch/main/graph/badge.svg?token=lKuTHVY3Ks" alt="Code Coverage"/>
  </a>
  <!-- NPM Version -->
  <a href="https://npmjs.org/package/relative-import-path">
    <img src="https://img.shields.io/npm/v/relative-import-path.svg" alt="NPM version"/>
  </a>
  <!-- Downloads -->
  <a href="https://npmjs.org/package/relative-import-path">
    <img src="https://img.shields.io/npm/dm/relative-import-path.svg" alt="Downloads"/>
  </a>
</h1>

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

```console
npm install relative-import-path
```

# API

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

# Examples

```javascript
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](./.github/CODE_OF_CONDUCT.md). By participating in this project you agree to abide by its terms.

---
_Source: https://npm.io/package/relative-import-path · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
