pagep
pagep

Reputation: 3629

Webpack - do not insert errors on missing module

When webpack can't process require it inserts error into the code instead.

Creating from this code:

var binding = require('./src/build/' + builds[i] + '/DTraceProviderBindings');

This code:

var binding = !(function webpackMissingModule() { var e = new Error("Cannot find module 'undefined'"); e.code = 'MODULE_NOT_FOUND'; throw e; }());

How to stop webpack from inserting the new Error ...?

This code is from dtrace-provider library. And indeed it's invalid as far as I know, that folder/file doesn't exist. I guess it's a bug in there. The problem is that when we left the code in (the code webpackMissingModule()) we start getting unexpected errors inside our application. Removing that line from the final bundle fixes our issues.

Problem is that we are not using dtrace-provider library ourselfs. It's a dependency of at least 3 other libs we use. So I guess forking it and changing it ourselves isn't good solution.

Our reduced webpack.production.js file:

const merge = require('webpack-merge');    
const common = require('./webpack.common');

module.exports = merge(common, {
  mode: 'production',
  optimization: {
    minimize: false
  }
});

I tried to use ignore plugin but it doesn't help / maybe I didn't have the correct regex. I tried to use the replace plugin, it didn't help. Don't know why but I was not able to get rid of that error message. The current work-around solution is to run simple script after build is done which replaces the line aka "build": "webpack --config webpack.production.js && node fixProduction.js",

Also accepting any other solution to the problem. Thanks

Upvotes: 3

Views: 713

Answers (1)

artempetrovcode
artempetrovcode

Reputation: 61

You could inject a dummy require function into the module using imports-loader:

module: {
  rules: [
    {
      test: require.resolve('node-dtrace-provider'),
      loader: 'imports-loader?require=>function() { return {}; }'
    }
  ]
}

The final bundle would look like this:

function() {
  var require = function() { return {}; };
  ...
  var binding = require('./src/build/' + builds[i] + '/DTraceProviderBindings');
  ...
}

Upvotes: 1

Related Questions