# babel-plugin-root-require

> Transforms require module paths that have a prefix referring to the project root. Also works on `require.resolve`.

Latest version **1.3.0** (published 2017-03-03) · CC0-1.0 license · 0 weekly downloads

## Install

```sh
npm install babel-plugin-root-require
pnpm add babel-plugin-root-require
yarn add babel-plugin-root-require
bun add babel-plugin-root-require
```

## 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.3.0 |
| Published | 2017-03-03 |
| First published | 2016-09-06 |
| Weekly downloads | 0 |
| License | CC0-1.0 |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2 |
| Author | Johnny Hauser |
| Maintainers | m59 |
| Keywords | babel, plugin, require, relative, project, root, path, simple, convenient |

## Links

- npm: https://www.npmjs.com/package/babel-plugin-root-require
- Repository: https://github.com/m59peacemaker/babel-plugin-root-require
- Homepage: https://github.com/m59peacemaker/babel-plugin-root-require#readme
- Issues: https://github.com/m59peacemaker/babel-plugin-root-require/issues
- npm.io page: https://npm.io/package/babel-plugin-root-require

## Dependencies (1)

- [root-import-utils](https://npm.io/package/root-import-utils.md) ^1.1.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.3.0 (latest) — 2017-03-03
- 1.2.0 — 2017-03-03
- 1.1.3 — 2017-02-17
- 1.1.2 — 2017-02-17
- 1.1.1 — 2017-02-16
- 1.1.0 — 2017-02-16
- 1.0.5 — 2017-02-09
- 1.0.4 — 2016-11-13
- 1.0.3 — 2016-09-06
- 1.0.2 — 2016-09-06
- 1.0.1 — 2016-09-06
- 1.0.0 — 2016-09-06

## README

# babel-plugin-root-require

Transforms require module paths that have a prefix referring to the project root. Also works on `require.resolve`.

## install

```sh
npm install babel-plugin-root-require
```

## example

### file structure

- src (project root)
  - foo
    - a.js
  - bar
    - b.js
    - baz
      - c.js
  - d.js

### javascript

```js
// these paths will work from any file in the project root (src)
require('~/foo/a')
require('~/bar/b')
require('~/bar/baz/c')
require('~/d')

// foo/a.js
require('~/d') // -> require('../d')
```

### .babelrc

```json
{
  "plugins": [
    ["root-require", {
      "projectRoot": "src"
    }]
  ]
}
```

### terminal

```sh
babel src --out-dir build
```

## options

### prefix = '~'

Prefix that refers to the project root

### projectRoot = process.cwd()

Absolute path of project root or relative path from process.cwd() to the project root which `prefix` refers to. Module paths will not be resolved correctly if this option is not correct. In the typical case that the source code is under the `src` directory, but babel is executed in the parent directory of `src`, set `projectRoot: src`.

### long = false

By default, transformed module paths will be the most efficient path from the requiring file to the module. When `long` is `true`, the prefix will be transformed to the relative path up the tree to the project root and the rest of the path is untouched. In some cases, the module path will be longer this way, because the path goes up the tree to the project root and then back down the tree to the module.

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