# webpack-runtime-require

> Exposes a way to require module exports at runtime. (eg. in browser consoles)

Latest version **2.0.0** (published 2024-04-03) · MIT license · 0 weekly downloads

## Install

```sh
npm install webpack-runtime-require
pnpm add webpack-runtime-require
yarn add webpack-runtime-require
bun add webpack-runtime-require
```

## Health

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

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.0 |
| Published | 2024-04-03 |
| First published | 2017-03-27 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 41 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 28 |
| Author | Venryx |
| Maintainers | venryx |
| Keywords | react |

## Links

- npm: https://www.npmjs.com/package/webpack-runtime-require
- Repository: https://github.com/Venryx/webpack-runtime-require
- Issues: https://github.com/Venryx/webpack-runtime-require/issues
- npm.io page: https://npm.io/package/webpack-runtime-require

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 2.0.0 (latest) — 2024-04-03
- 1.0.0 — 2021-06-18
- 0.3.3 — 2021-06-17
- 0.3.2 — 2021-06-14
- 0.3.1 — 2018-11-06
- 0.3.0 — 2018-04-25
- 0.2.0 — 2018-04-23
- 0.1.1 — 2017-11-13
- 0.1.0 — 2017-04-11
- 0.0.8 — 2017-03-28
- 0.0.7 — 2017-03-28
- 0.0.6 — 2017-03-28
- 0.0.5 — 2017-03-28
- 0.0.4 — 2017-03-28
- 0.0.3 — 2017-03-27
- … 2 more at https://npm.io/package/webpack-runtime-require/versions

## README

# Webpack Runtime Require

Exposes a way to require module exports at runtime. (eg. in browser consoles)

Supports Webpack versions 1, 2, 3?, 4, and 5.

### Install

```
npm install --save webpack-runtime-require
```

### Usage

* 1\) Tweak your `webpack.config.js` file, to expose the information necessary for "runtime module requiring/importing".
	* 1.1\) Ensure that one of the following are set, to have webpack provide module path info: (otherwise this library falls back to guessing the module's name from the variables where it's imported)
		* 1.1.A) Set `config.optimization.moduleIds: "named"` (in wp <=4: `config.optimization.namedModules: true`). [this is the cleanest way; and it's enabled by default when `config.mode == "development"`]
		* 1.1.B) Set `config.output.pathinfo: true`. [not as clean, since relies on regular-expressions, but should work]
	* 1.2\) The setting tweaks below are only relevant for prod builds, and only if you're using a recent webpack version. (where prod builds have these optimizations enabled by default; ie. no need to tweak these if WRR usage is undesired in prod)
		* 1.2.1\) Recommended config, if you want WRR usable in prod:
			```js
			// disable concatenation/merging of modules, even in prod
			// (otherwise webpack merges many modules, causing exports between them to be removed/privatized)
			config.optimization.concatenateModules = false;
			```
		* 1.2.2\) You *may* also want to tell webpack prod-builds to never remove unused exports -- for example, if there's a class that is only used in a single file, but you still want runtime access to inspect static variables or the like. To do so:
			```js
			// disable removal/privatizing of unused exports, even in prod
			// (otherwise webpack-runtime-require can't access unused-from-other-module exports)
			config.optimization.usedExports = false;
			```
		* Note: These two tweaks will likely increase the size of your builds slightly (in my project, by ~10%). So weigh whether the slight size increase is worth it relative to improved inspection/debugging in your prod builds.

* 2\) Add some way for the library to access the webpack data.

	This is generally as simple as importing it from one of the files in your main project/chunk:
	```
	import "webpack-runtime-require";
	```
	If that fails, try the following: (make sure the below runs before any uses of the `wrr` fields/functions)
	```
	import {Init} from "webpack-runtime-require";

	// if you're using typescript, uncomment the line below
	//declare var __webpack_require__, __webpack_modules__, __webpack_module_cache__;

	Init({
		__webpack_require__,
		// omit the two below if on Webpack 4 or below
		__webpack_modules__: __webpack_modules__, // don't collapse this; can confuse babel/webpack
		__webpack_module_cache__,
	});
	```
	(If even that fails, try using one of the older versions of webpack-runtime-require [0.3.3 or older], which has a couple other approaches for finding/supplying the data.)

	After importing/initialization, the library will create a `window.wrr` object. This object exposes structures and functions for accessing the contents of any module within the Webpack bundle.

* 3\) Use the `window.wrr` object in dev-tools (or the `wrr` export in compile-time code) to access module contents.
	* 3.A\) A dev-tools example, of importing based on module/file name:
		```
		let React = wrr.Module("react");
		console.log("Retrieved Component class from react:", React.Component);

		// if path was "./Path/To/MyComponent", specify just the file-name
		let MyComponent = wrr.Module("MyComponent");
		console.log("Retrieved MyComponent:", MyComponent);

		// if you want to import by path instead of file-name, see here:
		//   https://github.com/Venryx/webpack-runtime-require/issues/3#issuecomment-864035793
		```
	* 3.B\) A dev-tools example, of importing based on export name:
		```
		wrr.Start();
		let Component = wrr.Export("Component"); // retrieved from the "react" library
		console.log("Retrieved Component class from react:", Component);
		```
	* 3.C\) A compile-time code example, of doing some of the operations above:
		```
		import {wrr} from "webpack-runtime-require";
		console.log("Retrieved Component class from react:", wrr.Export("Component"));
		console.log("All react exports:", Object.entries(wrr.Module("react")));
		```

### Additional usage

If you're using the dev-tools console, you can see autocomplete for the modules:

* 1\) Type and run: `wrr.Start()` (this triggers the module finding/processing)
* 2\) Type: `wrr.moduleExports.` (or `wrr.moduleExports_byShortName.`)

You should then see all the modules in your app displayed in the autocomplete dropdown.

Other useful contents of the `window.wrr` structure:
* `wrr.moduleExports_flat`: Contains the exports from every module, stored by *export name* rather than module name.
* `wrr.webpackData`: The raw data-structures retrieved from webpack.

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