Skip to content
Rens Baardman edited this page Jun 12, 2019 · 6 revisions

Troubleshooting

This is collection of various errors and warnings that you can encounter when using any variation (or lack of) rewire, webpack, rewire-webpack (the original version), the fork dribba/rewire-webpack and this fork rewire-webpack-plugin.

Bundling a script with webpack that requires rewire, but without using any plugins

$ webpack

Hash: 9041342f61ee15f58a1a
Version: webpack 4.33.0
Time: 4080ms
Built at: 06/12/2019 12:30:15 PM
  Asset     Size  Chunks             Chunk Names
null.js  5.4 MiB    null  [emitted]  null
Entrypoint null = null.js
[0] util (ignored) 15 bytes {null} [built]
[1] util (ignored) 15 bytes {null} [built]
[2] buffer (ignored) 15 bytes {null} [optional] [built]
[3] crypto (ignored) 15 bytes {null} [optional] [built]
[./node_modules/eslint/lib sync recursive] ./node_modules/eslint/lib sync 160 bytes {null} [built]
[./node_modules/eslint/lib/config sync recursive] ./node_modules/eslint/lib/config sync 160 bytes {null} [built]
[./node_modules/eslint/lib/config sync recursive ^.*\/package\.json$] ./node_modules/eslint/lib/config sync ^.*\/package\.json$ 160 bytes {null} [optional] [built]
[./node_modules/import-fresh sync recursive] ./node_modules/import-fresh sync 160 bytes {null} [built]
[./node_modules/webpack/buildin/global.js] (webpack)/buildin/global.js 472 bytes {null} [built]
[./node_modules/webpack/buildin/module.js] (webpack)/buildin/module.js 497 bytes {null} [built]
[./require-rewire.js] 68 bytes {null} [built]
    + 665 hidden modules

WARNING in ./node_modules/eslint/lib/linter.js 451:46-65
Critical dependency: the request of a dependency is an expression
 @ ./node_modules/eslint/lib/api.js
 @ ./node_modules/rewire/lib/moduleEnv.js
 @ ./node_modules/rewire/lib/rewire.js
 @ ./node_modules/rewire/lib/index.js
 @ ./require-rewire.js

... 8 similar warnings omitted ...

WARNING in ./node_modules/rewire/lib/moduleEnv.js
Module not found: Error: Can't resolve 'coffee-script' in 'xxx/node_modules/rewire/lib'
 @ ./node_modules/rewire/lib/moduleEnv.js
 @ ./node_modules/rewire/lib/rewire.js
 @ ./node_modules/rewire/lib/index.js
 @ ./require-rewire.js

... 1 similar warning omitted ...

WARNING in ./node_modules/rewire/lib/moduleEnv.js 85:34-52
require.extensions is not supported by webpack. Use a loader instead.
 @ ./node_modules/rewire/lib/rewire.js 6:16-41
 @ ./node_modules/rewire/lib/index.js
 @ ./require-rewire.js

... 5 similar warning omitted ...

ERROR in ./node_modules/eslint/lib/cli-engine.js
Module not found: Error: Can't resolve 'fs' in 'xxx/node_modules/eslint/lib'
 @ ./node_modules/eslint/lib/cli-engine.js 18:11-24
 @ ./node_modules/eslint/lib/api.js
 @ ./node_modules/rewire/lib/moduleEnv.js
 @ ./node_modules/rewire/lib/rewire.js
 @ ./node_modules/rewire/lib/index.js
 @ ./require-rewire.js

... 25 similar errors omitted ...

Solution

Add rewire-webpack-plugin to your webpack config:

// webpack.config.js

const RewireWebpackPlugin = require("rewire-webpack-plugin");
module.exports = {

	// ...

	plugins: [
		new RewireWebpackPlugin()
	]
};

Bundling with webpack, while using rewire-webpack as a plugin

This happens even if the script you are trying to bundle does not require rewire.

Cannot read property 'plugin' of undefined
(node:9306) DeprecationWarning: Tapable.plugin is deprecated. Use new API on `.hooks` instead

Solution

Replace rewire-webpack with rewire-webpack-plugin.

Bundling with webpack, while using dribba/rewire-webpack as a plugin

When using the fork dribba/rewire-webpack and the script you are trying to bundle does not require rewire:

(node:9904) DeprecationWarning: Tapable.plugin is deprecated. Use new API on `.hooks` instead
(node:9904) DeprecationWarning: webpack: Using compiler.resolvers.normal is deprecated.
Use compiler.resolverFactory.plugin("resolver normal", resolver => {
  resolver.apply(/* … */);
}); instead.
(node:9904) DeprecationWarning: Tapable.apply is deprecated. Call apply on the plugin directly instead
(node:9904) DeprecationWarning: Resolver: The callback argument was splitted into resolveContext and callback.
(node:9904) DeprecationWarning: Resolver#doResolve: The type arguments (string) is now a hook argument (Hook). Pass a reference to the hook instead.

Additional errors when you also try to call rewire() in your script:

TypeError: dep.getResourceIdentifier is not a function
	at ...

Solution

Replace dribba/rewire-webpack with rewire-webpack-plugin.

Running the generated bundle while rewire() was called inappropriately

This happens in any of the following situations (see README for limitations)

  • if the variable into which rewire was required is not called rewire
  • if you run rewire() on anything else than a plain string (e.g. a variable containing a string, or a template string)

In Firefox, it throw the following error:

TypeError: __webpack_require__.m[moduleId] is undefined (null.js line 97 > eval:16:2)

In Chrome:

Uncaught TypeError: Cannot read property 'call' of undefined (rewire.web.js:16)
    at rewire (rewire.web.js:16)
    at eval (dynamic-rewire.js:13)
    at Object../dynamic-rewire.js (null.js:108)
    at __webpack_require__ (null.js:20)
    at null.js:84
    at null.js:87

When running the generated code in Node.js:

webpack:///./lib/rewire.web.js?:16
	__webpack_require__.m[moduleId].call(
	                                ^

TypeError: Cannot read property 'call' of undefined
    at rewire (webpack:///./lib/rewire.web.js?:16:34)
    at ...

Solution

Correct usage:

const rewire = require('rewire');
const my_lib = rewire('./src/my_lib.js');