# path-prefix

> Create a relative path to a directory

Latest version **1.0.0** (published 2017-04-17) · MIT license · 0 weekly downloads

## Install

```sh
npm install path-prefix
pnpm add path-prefix
yarn add path-prefix
bun add path-prefix
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.0 |
| Published | 2017-04-17 |
| First published | 2017-04-17 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=4 |
| Dependencies | 1 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Geoff Kimball |
| Maintainers | gakimball |
| Keywords | path-prefix |

## Links

- npm: https://www.npmjs.com/package/path-prefix
- Repository: https://github.com/gakimball/path-prefix
- Homepage: https://github.com/gakimball/path-prefix#readme
- Issues: https://github.com/gakimball/path-prefix/issues
- npm.io page: https://npm.io/package/path-prefix

## Dependencies (1)

- [slash](https://npm.io/package/slash.md) ^1.0.0

## 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.0 (latest) — 2017-04-17

## README

# path-prefix

> Create a relative path to a directory

[![Travis](https://img.shields.io/travis/gakimball/path-prefix.svg?maxAge=2592000)](https://travis-ci.org/gakimball/path-prefix) [![npm](https://img.shields.io/npm/v/path-prefix.svg?maxAge=2592000)](https://www.npmjs.com/package/path-prefix)

Returns a string you can append to the beginning of a path to make it relative to another path. See [Example](#example) below for a use case.

## Installation

```bash
npm install path-prefix
```

## Usage

```js
const pathPrefix = require('path-prefix');

const fileA = '/path/to/file.html';
const fileB = '/path/to/subdirectory/file.html';
const root = '/path/to/file';

// No prefix is needed if the file is right inside the root
pathPrefix(fileA, root); // => ''

// If the file is in a different directory, a relative path prefix is made
pathPrefix(fileB, root); // => '../'
```

## Example

Use this if you're generating HTML and need all of your `<link>`, `<img>`, and `<script>` tags to be relative paths.

```js
const pathPrefix = require('path-prefix');

const path = '/src/about/index.html';
const root = '/src';

const link = `<link rel="stylesheet" href="${pathPrefix(path, root)}styles/style.css">`;
// => '<link rel="stylesheet" href="../styles/style.css">'
```

## API

### pathPrefix(file, root)

Create a string that can be prepended to a file path that needs to resolve to a root directory.

- **file** (String): Path to the file.
- **root** (String): Path to the root directory.

Returns an empty string if the file is at the root, or a series of `../` characters if the file is in a subdirectory relative to the root.

## Local Development

```bash
git clone https://github.com/gakimball/path-prefix
cd path-prefix
npm install
npm test
```

## License

MIT &copy; [Geoff Kimball](http://geoffkimball.com)

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