TypeError: Cannot read property ‘tap’ of undefined in NormalModuleReplacementPlugin

Bug report

Trying to implement the NormalModuleReplacementPlugin in my webpack config:

resolve: {
    plugins: [
        new webpack.NormalModuleReplacementPlugin(/something/, function (resource) {
            //do something here
        })
    ]
}

What is the current behavior?

/[FULL_PATH_TO_PROJECT]/node_modules/webpack/lib/NormalModuleReplacementPlugin.js:18
		compiler.hooks.normalModuleFactory.tap(
		                                   ^

TypeError: Cannot read property 'tap' of undefined
    at NormalModuleReplacementPlugin.apply (/[FULL_PATH_TO_PROJECT]/node_modules/webpack/lib/NormalModuleReplacementPlugin.js:18:38)
    at plugins.forEach.plugin (/[FULL_PATH_TO_PROJECT]/node_modules/webpack/node_modules/enhanced-resolve/lib/ResolverFactory.js:276:10)
    at Array.forEach (<anonymous>)
    at Object.exports.createResolver (/[FULL_PATH_TO_PROJECT]/node_modules/webpack/node_modules/enhanced-resolve/lib/ResolverFactory.js:275:10)
    at ResolverFactory._create (/[FULL_PATH_TO_PROJECT]/node_modules/webpack/lib/ResolverFactory.js:57:28)
    at ResolverFactory.get (/[FULL_PATH_TO_PROJECT]/node_modules/webpack/lib/ResolverFactory.js:50:28)
    at NormalModuleFactory.getResolver (/[FULL_PATH_TO_PROJECT]/node_modules/webpack/lib/NormalModuleFactory.js:519:31)
    at /[FULL_PATH_TO_PROJECT]/node_modules/webpack/lib/NormalModuleFactory.js:165:32
    at /[FULL_PATH_TO_PROJECT]/node_modules/webpack/lib/NormalModuleFactory.js:129:4
    at hooks.beforeResolve.callAsync (/[FULL_PATH_TO_PROJECT]/node_modules/webpack/lib/NormalModuleFactory.js:396:5)
    at AsyncSeriesWaterfallHook.eval [as callAsync] (eval at create (/[FULL_PATH_TO_PROJECT]/node_modules/webpack/node_modules/tapable/lib/HookCodeFactory.js:24:12), <anonymous>:6:1)
    at AsyncSeriesWaterfallHook.lazyCompileHook [as _callAsync] (/[FULL_PATH_TO_PROJECT]/node_modules/webpack/node_modules/tapable/lib/Hook.js:35:21)
    at NormalModuleFactory.create (/[FULL_PATH_TO_PROJECT]/node_modules/webpack/lib/NormalModuleFactory.js:377:28)
    at semaphore.acquire (/[FULL_PATH_TO_PROJECT]/node_modules/webpack/lib/Compilation.js:941:18)
    at Semaphore.acquire (/[FULL_PATH_TO_PROJECT]/node_modules/webpack/lib/util/Semaphore.js:29:4)
    at Compilation._addModuleChain (/[FULL_PATH_TO_PROJECT]/node_modules/webpack/lib/Compilation.js:940:18)
    at Compilation.addEntry (/[FULL_PATH_TO_PROJECT]/node_modules/webpack/lib/Compilation.js:1045:8)
    at compiler.hooks.make.tapAsync (/[FULL_PATH_TO_PROJECT]/node_modules/webpack/lib/SingleEntryPlugin.js:46:17)
    at AsyncParallelHook.eval [as callAsync] (eval at create (/[FULL_PATH_TO_PROJECT]/node_modules/webpack/node_modules/tapable/lib/HookCodeFactory.js:24:12), <anonymous>:13:1)
    at AsyncParallelHook.lazyCompileHook [as _callAsync] (/[FULL_PATH_TO_PROJECT]/node_modules/webpack/node_modules/tapable/lib/Hook.js:35:21)
    at hooks.beforeCompile.callAsync.err (/[FULL_PATH_TO_PROJECT]/node_modules/webpack/lib/Compiler.js:542:20)
    at AsyncSeriesHook.eval [as callAsync] (eval at create (/[FULL_PATH_TO_PROJECT]/node_modules/webpack/node_modules/tapable/lib/HookCodeFactory.js:24:12), <anonymous>:6:1)
    at AsyncSeriesHook.lazyCompileHook [as _callAsync] (/[FULL_PATH_TO_PROJECT]/node_modules/webpack/node_modules/tapable/lib/Hook.js:35:21)
    at Compiler.compile (/[FULL_PATH_TO_PROJECT]/node_modules/webpack/lib/Compiler.js:535:28)
    at readRecords.err (/[FULL_PATH_TO_PROJECT]/node_modules/webpack/lib/Compiler.js:274:11)
    at Compiler.readRecords (/[FULL_PATH_TO_PROJECT]/node_modules/webpack/lib/Compiler.js:402:11)
    at hooks.run.callAsync.err (/[FULL_PATH_TO_PROJECT]/node_modules/webpack/lib/Compiler.js:271:10)
    at AsyncSeriesHook.eval [as callAsync] (eval at create (/[FULL_PATH_TO_PROJECT]/node_modules/webpack/node_modules/tapable/lib/HookCodeFactory.js:24:12), <anonymous>:6:1)
    at AsyncSeriesHook.lazyCompileHook [as _callAsync] (/[FULL_PATH_TO_PROJECT]/node_modules/webpack/node_modules/tapable/lib/Hook.js:35:21)
    at hooks.beforeRun.callAsync.err (/[FULL_PATH_TO_PROJECT]/node_modules/webpack/lib/Compiler.js:268:19)
    at AsyncSeriesHook.eval [as callAsync] (eval at create (/[FULL_PATH_TO_PROJECT]/node_modules/webpack/node_modules/tapable/lib/HookCodeFactory.js:24:12), <anonymous>:15:1)
    at AsyncSeriesHook.lazyCompileHook [as _callAsync] (/[FULL_PATH_TO_PROJECT]/node_modules/webpack/node_modules/tapable/lib/Hook.js:35:21)
    at Compiler.run (/[FULL_PATH_TO_PROJECT]/node_modules/webpack/lib/Compiler.js:265:24)
    at runWithDependencies (/[FULL_PATH_TO_PROJECT]/node_modules/webpack/lib/MultiCompiler.js:258:15)
    at asyncLib.map (/[FULL_PATH_TO_PROJECT]/node_modules/webpack/lib/MultiCompiler.js:178:6)
    at arrayEachIndex (/[FULL_PATH_TO_PROJECT]/node_modules/neo-async/async.js:2539:9)

If the current behavior is a bug, please provide the steps to reproduce.

What is the expected behavior?

I’d expect the NormalModuleReplacementPlugin to work, and execute the code in the callback.
Not throw this error. It’s not behaving as in the documents.

Other relevant information:
webpack version: 4.17.1
Node.js version: v8.9.4
Operating System: macOs 10.12.6
Additional tools:

Author: Fantashit

1 thought on “TypeError: Cannot read property ‘tap’ of undefined in NormalModuleReplacementPlugin

Comments are closed.