# import-from

> Import a module like with `require()` but from a given path

Latest version **4.0.0** (published 2021-06-01) · MIT license · 0 weekly downloads

## Install

```sh
npm install import-from
pnpm add import-from
yarn add import-from
bun add import-from
```

## 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 | 4.0.0 |
| Published | 2021-06-01 |
| First published | 2017-05-21 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=12.2 |
| Dependencies | 0 |
| Unpacked size | 4.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 85 |
| Author | Sindre Sorhus |
| Maintainers | sindresorhus |
| Keywords | require, resolve, path, module, from, like, import, path |

## Links

- npm: https://www.npmjs.com/package/import-from
- Repository: https://github.com/sindresorhus/import-from
- Homepage: https://github.com/sindresorhus/import-from#readme
- Issues: https://github.com/sindresorhus/import-from/issues
- Funding: https://github.com/sponsors/sindresorhus
- npm.io page: https://npm.io/package/import-from

## 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

- 4.0.0 (latest) — 2021-06-01
- 3.0.0 — 2019-04-27
- 2.1.0 — 2017-05-21

## README

# import-from

> Import a module like with [`require()`](https://nodejs.org/api/modules.html#modules_require_id) but from a given path

## Install

```
$ npm install import-from
```

## Usage

```js
const importFrom = require('import-from');

// There is a file at `./foo/bar.js`

importFrom('foo', './bar');
```

## API

### importFrom(fromDirectory, moduleId)

Like `require()`, throws when the module can't be found.

### importFrom.silent(fromDirectory, moduleId)

Returns `undefined` instead of throwing when the module can't be found.

#### fromDirectory

Type: `string`

Directory to import from.

#### moduleId

Type: `string`

What you would use in `require()`.

## Tip

Create a partial using a bound function if you want to import from the same `fromDir` multiple times:

```js
const importFromFoo = importFrom.bind(null, 'foo');

importFromFoo('./bar');
importFromFoo('./baz');
```

## Related

- [import-cwd](https://github.com/sindresorhus/import-cwd) - Import a module from the current working directory
- [resolve-from](https://github.com/sindresorhus/resolve-from) - Resolve the path of a module from a given path
- [resolve-cwd](https://github.com/sindresorhus/resolve-cwd) - Resolve the path of a module from the current working directory
- [resolve-pkg](https://github.com/sindresorhus/resolve-pkg) - Resolve the path of a package regardless of it having an entry point
- [import-lazy](https://github.com/sindresorhus/import-lazy) - Import modules lazily
- [import-global](https://github.com/sindresorhus/import-global) - Import a globally installed module

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