rewire-webpack icon indicating copy to clipboard operation
rewire-webpack copied to clipboard

Module not found: Error: Cannot resolve module 'module' in c:\app\node_modules\rewire\lib

Open joyfulelement opened this issue 9 years ago • 10 comments

Hi:

I'm trying to put together a react web app with Webpack + Karma that runs the Jasmine unit test spec with rewire-webpack. Whenever I tried running the Karma, the following error occurred:

WARNING in ./~/rewire/lib/moduleEnv.js
require.extensions is not supported by webpack. Use a loader instead.

WARNING in ./~/rewire/lib/moduleEnv.js
require.extensions is not supported by webpack. Use a loader instead.

WARNING in ./~/rewire/lib/moduleEnv.js
require.extensions is not supported by webpack. Use a loader instead.

WARNING in ./~/rewire/lib/moduleEnv.js
Module not found: Error: Cannot resolve module 'coffee-script' in c:\app\node_modules\rewire\lib
 @ ./~/rewire/lib/moduleEnv.js 82:13-37

ERROR in ./~/rewire/lib/rewire.js
Module not found: Error: Cannot resolve module 'module' in c:\app\node_modules\rewire\lib
 @ ./~/rewire/lib/rewire.js 1:13-30

ERROR in ./~/rewire/lib/rewire.js
Module not found: Error: Cannot resolve module 'fs' in c:\app\node_modules\rewire\lib
 @ ./~/rewire/lib/rewire.js 2:9-22

ERROR in ./~/rewire/lib/moduleEnv.js
Module not found: Error: Cannot resolve module 'module' in c:\app\node_modules\rewire\lib
 @ ./~/rewire/lib/moduleEnv.js 3:13-30

ERROR in ./~/rewire/lib/moduleEnv.js
Module not found: Error: Cannot resolve module 'fs' in c:\app\node_modules\rewire\lib
 @ ./~/rewire/lib/moduleEnv.js 4:9-22
..
Chrome 47.0.2526 (Windows 8 0.0.0) ERROR
 Uncaught Error: Cannot find module "module"
 at c:/app/src/js/components/__tests__/reactApp.spec.js:14137

The following is the configuration for karma.conf.js:

var webpack = require("webpack");
var RewirePlugin = require('rewire-webpack');
var rewirePlugin = new RewirePlugin();

module.exports = function (config) {
    config.set({

        // base path that will be used to resolve all patterns (eg. files, exclude)
        basePath: '',

        // frameworks to use
        frameworks: ['jasmine'],

        // list of files / patterns to load in the browser
        files: [
            '../../src/js/**/__tests__/*spec.js'
        ],

        preprocessors: {
            '../../src/js/**/*.js': ['webpack'],
            '../../src/js/**/__tests__/*spec.js': ['webpack']
        },

        webpack: {
            module: {
                loaders: [
                    {test: /\.js$/, loader: 'babel-loader'}
                ]
            },
            devtool: 'inline-source-map'
        },

        ...

        // List plugins explicitly
        plugins: [
            ...
            rewirePlugin
        ]
    })
}

Currently using babel and babel loader version > 6 as defined in package.json

  "devDependencies": {
    "babel-core": "^6.1.2",
    "babel-loader": "^6.0.1",
    "babel-runtime": "^6.0.14",
    "jasmine-core": "^2.3.4",
    "karma": "^0.13.15",
    "karma-chrome-launcher": "^0.2.1",
    "karma-cli": "^0.1.1",
    "karma-webpack": "^1.7.0",
    "rewire": "^2.4.0",
    "rewire-webpack": "^1.0.0",
    "webpack": "^1.12.2",

  },

Does anyone happen to know if such configuration with the version lineup here with babel + webpack would work with rewire-webpack?

Many Thanks!

joyfulelement avatar Nov 08 '15 23:11 joyfulelement

+1

ppoliani avatar Nov 09 '15 22:11 ppoliani

Having exact same issue but with Mocha.

stuartmemo avatar Dec 30 '15 20:12 stuartmemo

+1 Mocha

DerekStrickland avatar Feb 19 '16 17:02 DerekStrickland

Has anyone seen any progress on this? I'm having the same issue, using mocha.

MagicIndustries avatar Apr 04 '16 11:04 MagicIndustries

1+ Mocha

marcelorl avatar Apr 06 '16 20:04 marcelorl

I'm having a near identical problem, the output is:

Module not found: Error: Cannot resolve module 'module' in ...blah...blah.../node_modules/prunk

christhomas avatar Apr 11 '16 09:04 christhomas

Isn't the plugin meant to go in the webpack config, rather than the karma config?

joejuzl avatar Jun 08 '16 16:06 joejuzl

I'm having the same issue. Are there're any thoughts on how to make possible bundling tests with rewire for test purposes?

oleggromov avatar Jun 16 '16 11:06 oleggromov

I ended up using this instead: https://github.com/speedskater/babel-plugin-rewire

joejuzl avatar Jun 16 '16 16:06 joejuzl

This is a late update, but for anyone still encountering this problem: note that the plugin has to be defined as a webpack plugin, not as a Karma plugin!

It should look similar to this:

// karma.conf.js

const RewirePlugin = require('rewire-webpack');

module.exports = function (config) {
    config.set({

        ...

        webpack: {
            ...
            plugins: [ new RewirePlugin() ]
        }

    });
}

Note that since rewire-webpack unfortunately doesn't have webpack 4 support, you will still get errors like this:

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

To solve this, you can use my fork rensbaardman/rewire-webpack-plugin, which adds webpack 4 support.

rensbaardman avatar Jun 12 '19 13:06 rensbaardman