Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
joyfulelement opened this issue Nov 8, 2015 · 10 comments

Comments

@joyfulelement
Copy link

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
Copy link

ppoliani commented Nov 9, 2015

+1

@stuartmemo
Copy link

Having exact same issue but with Mocha.

@DerekStrickland
Copy link

+1 Mocha

@MagicIndustries
Copy link

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

@marcelorl
Copy link

1+ Mocha

@christhomas
Copy link

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
Copy link

joejuzl commented Jun 8, 2016

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

@oleggromov
Copy link

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

@joejuzl
Copy link

joejuzl commented Jun 16, 2016

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

@rensbaardman
Copy link

rensbaardman commented Jun 12, 2019

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants