jhnns / rewire-webpack

Dependency injection for webpack bundles
The Unlicense
121 stars 20 forks source link

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

Open joyfulelement opened 9 years ago

joyfulelement commented 9 years ago

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!

ppoliani commented 9 years ago

+1

stuartmemo commented 8 years ago

Having exact same issue but with Mocha.

DerekStrickland commented 8 years ago

+1 Mocha

MagicIndustries commented 8 years ago

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

marcelorl commented 8 years ago

1+ Mocha

christhomas commented 8 years ago

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

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

joejuzl commented 8 years ago

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

oleggromov commented 8 years ago

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

joejuzl commented 8 years ago

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

rensbaardman commented 5 years ago

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.