# bundle-require

> bundle and require a file

Latest version **5.1.0** (published 2024-12-22) · MIT license · 0 weekly downloads

## Install

```sh
npm install bundle-require
pnpm add bundle-require
yarn add bundle-require
bun add bundle-require
```

## Health

**Score 40/100 (D)** — status: maintenance-mode.

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

Warnings: low downloads.

Negative: stale; low maintenance score.

## Facts

| | |
|---|---|
| Version | 5.1.0 |
| Published | 2024-12-22 |
| First published | 2021-10-09 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | ^12.20.0 \|\| ^14.13.1 \|\| >=16.0.0 |
| Dependencies | 1 |
| Unpacked size | 25.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Maintainers | egoist |

## Links

- npm: https://www.npmjs.com/package/bundle-require
- npm.io page: https://npm.io/package/bundle-require

## Dependencies (1)

- [load-tsconfig](https://npm.io/package/load-tsconfig.md) ^0.2.3

## Recent versions

- 5.1.0 (latest) — 2024-12-22
- 5.0.0 — 2024-06-01
- 4.2.1 — 2024-06-01
- 4.2.0 — 2024-06-01
- 4.1.0 — 2024-05-02
- 4.0.4 — 2024-05-02
- 4.0.3 — 2024-04-22
- 4.0.2 — 2023-09-25
- 4.0.1 — 2023-02-09
- 4.0.0 — 2023-02-07
- 3.1.2 — 2022-11-03
- 3.1.1 — 2022-11-03
- 3.1.0 — 2022-08-26
- 3.0.5 — 2022-08-26
- 3.0.4 — 2022-01-30
- … 28 more at https://npm.io/package/bundle-require/versions

## README

**💛 You can help the author become a full-time open-source maintainer by [sponsoring him on GitHub](https://github.com/sponsors/egoist).**

---

# bundle-require

[![npm version](https://badgen.net/npm/v/bundle-require)](https://npm.im/bundle-require) [![npm downloads](https://badgen.net/npm/dm/bundle-require)](https://npm.im/bundle-require) [![jsDocs.io](https://img.shields.io/badge/jsDocs.io-reference-blue)](https://www.jsdocs.io/package/bundle-require)

## Use Case

Projects like [Vite](https://vitejs.dev) need to load config files provided by the user, but you can't do it with just `require()` because it's not necessarily a CommonJS module, it could also be a `.mjs` or even be written in TypeScript, and that's where the `bundle-require` package comes in, it loads the config file regardless what module format it is.

## How it works

- Bundle your file with esbuild, `node_modules` are excluded because it's problematic to try to bundle it
  - `__filename`, `__dirname` and `import.meta.url` are replaced with source file's value instead of the one from the temporary output file
- Output file in `esm` format if possible (for `.ts`, `.js` input files)
- Load output file with `import()` if possible
- Return the loaded module and its dependencies (imported files)

## Install

```bash
npm i bundle-require esbuild
```

`esbuild` is a peer dependency.

## Usage

```ts
import { bundleRequire } from 'bundle-require'

const { mod } = await bundleRequire({
  filepath: './project/vite.config.ts',
})
```

## API

https://www.jsdocs.io/package/bundle-require

## Projects Using bundle-require

Projects that use **bundle-require**:

- [VuePress](https://github.com/vuejs/vuepress): :memo: Minimalistic Vue-powered static site generator.

## Sponsors

[![sponsors](https://sponsors-images.egoist.dev/sponsors.svg)](https://github.com/sponsors/egoist)

## License

MIT &copy; [EGOIST](https://github.com/sponsors/egoist)

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