# @tybys/native-require

> Try to get native require function in any environment.

Latest version **3.0.3** (published 2021-05-08) · MIT license · 0 weekly downloads

## Install

```sh
npm install @tybys/native-require
pnpm add @tybys/native-require
yarn add @tybys/native-require
bun add @tybys/native-require
```

## Health

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

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

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 3.0.3 |
| Published | 2021-05-08 |
| First published | 2020-04-26 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 16.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | toyobayashi |
| Maintainers | toyobayashi |

## Links

- npm: https://www.npmjs.com/package/@tybys/native-require
- Repository: https://github.com/toyobayashi/native-require
- Homepage: https://github.com/toyobayashi/native-require#readme
- Issues: https://github.com/toyobayashi/native-require/issues
- npm.io page: https://npm.io/package/@tybys/native-require

## Recent versions

- 3.0.3 (latest) — 2021-05-08
- 3.0.2 — 2021-05-08
- 3.0.1 — 2021-01-15
- 3.0.0 — 2020-10-18
- 2.0.2 — 2020-10-02
- 2.0.1 — 2020-09-29
- 2.0.0 — 2020-09-02
- 1.3.1 — 2020-06-30
- 1.3.0 — 2020-06-30
- 1.2.0 — 2020-06-28
- 1.1.0 — 2020-04-26
- 1.0.1 — 2020-04-26
- 1.0.0 — 2020-04-26

## README

# native-require

Try to get native require function in any environment.

``` bash
$ npm install @tybys/native-require
```

## When should you use this package

If you want to write a library which supports both Node.js and browser, and you hope it can be work well with webpack, you can use this package instead of writing `__non_webpack_require__` directly in your code.

## Usage:

### Webpack

Plugin is **not** neccesary.

``` js
const { NativeRequireWebpackPlugin } = require('@tybys/native-require/plugins/webpack.js')

module.exports = {
  /* ... */
  plugins: [
    new NativeRequireWebpackPlugin()
  ]
}
```

CommonJS:

``` js
const { tryGetRequireFunction } = require('@tybys/native-require/index.js') // index.js can not be omitted
const nativeRequire = tryGetRequireFunction()
if (typeof nativeRequire === 'function') {
  // ...
}
```

ESM format input / TypeScript:

``` ts
import { tryGetRequireFunction } from '@tybys/native-require'
const nativeRequire = tryGetRequireFunction()
if (typeof nativeRequire === 'function') {
  // ...
}
```

### Rollup

**NOTE**: If you are using commonjs `require('@tybys/native-require/index.js')` with `@rollup/plugin-commonjs`, you need to add `@tybys/native-require/plugins/rollup.js`.

``` js
const { nativeRequireRollupPlugin } = require('@tybys/native-require/plugins/rollup.js')

module.exports = {
  plugins: [
    nativeRequireRollupPlugin(),
    /* commonjs node-resolve ... */
  ]
}
```

``` js
const { tryGetRequireFunction } = require('@tybys/native-require/index.js') // index.js can not be omitted
```

ESM / TypeScript:

``` js
import { tryGetRequireFunction } from '@tybys/native-require'
const nativeRequire = tryGetRequireFunction()
if (typeof nativeRequire === 'function') {
  // ...
}
```

### Browser (Generally do not use) / Electron renderer process

``` html
<script src="node_modules/@tybys/native-require/dist/native-require.js"></script>
<script>
  (function () {
    var nativeRequire = nr.tryGetRequireFunction(typeof module !== 'undefined' ? module : undefined)();
    if (typeof nativeRequire === 'function') {
      // ...
    }
  })();
</script>
```

Examples are in `test` folder.

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