9

With require.js it was very easy to debug a module in Chrome's DevTools, by simply entering:

require('my-module').callThisFunction()

With Webpack this is not possible anymore, because it compiles the modules via CLI and does not export require.

window.webpackJsonp is globally exposed, so I thought I could just find the module ID and call it like this: webpackJsonp([1],[]), but unfortunately this returns undefined.

Are there any workarounds to still be able to debug like require.js?

1

2 Answers 2

1

Add code to module in bundle

require.ensure([], function() {
  window.require = function(smth) {
    return require('./' + smth);
  };
});

Now you can use 'require' from chrome console like require('app').doSmth()

Sign up to request clarification or add additional context in comments.

Comments

1

You can get something pretty close using expose-loader. Ie. for React you could have { test: require.resolve("react"), loader: "expose?React" } at your loader configuration. After that you can access React through console. The same idea applies for other libraries.

4 Comments

Yeah, but then I would need to do this for every module that I have (and it would create globals). This takes away the ease of testing that I had before with require.js.
That's definitely true! I've "solved" this problem by using hot loading. If I want to introspect something I just add a console.log somewhere. After that it triggers my build and updates the browser where I can see the answer I was after. Would that work for you?
Unfortunately no, because being able to freely test all modules in the console can really speed up debugging. Is there really no way to just do something like getWebpackModule(moduleId)?
As far as I understand, nope. Webpack just gives you a bundle. Fortunately Webpack's watcher works fast during development so that helps a bit. Btw, see blog.player.me/… . Some good info on moving from RequireJS to Webpack.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.