Maksym
Maksym

Reputation: 3428

Ignore missing dependencies

I have some issues with compiling my bundle. Basically I have webpack 1.13 and npm3 and when I try to bundle my files I get error that some dependencies are not found. More specifically I imported log4js package and I get

ERROR in ./~/log4js/lib/appenders/hipchat.js
Module not found: Error: Cannot resolve module 'hipchat-client' in c:\vhosts\not
ifications-daemons\node_modules\log4js\lib\appenders
 @ ./~/log4js/lib/appenders/hipchat.js 2:20-45

ERROR in ./~/log4js/lib/appenders/loggly.js
Module not found: Error: Cannot resolve module 'loggly' in c:\vhosts\notificatio
ns-daemons\node_modules\log4js\lib\appenders
 @ ./~/log4js/lib/appenders/loggly.js 3:11-28

ERROR in ./~/log4js/lib/appenders/mailgun.js
Module not found: Error: Cannot resolve module 'mailgun-js' in c:\vhosts\notific
ations-daemons\node_modules\log4js\lib\appenders
 @ ./~/log4js/lib/appenders/mailgun.js 5:14-35

ERROR in ./~/log4js/lib/appenders/slack.js
Module not found: Error: Cannot resolve module 'slack-node' in c:\vhosts\notific
ations-daemons\node_modules\log4js\lib\appenders
 @ ./~/log4js/lib/appenders/slack.js 2:12-33

ERROR in ./~/log4js/lib/appenders/smtp.js
Module not found: Error: Cannot resolve module 'nodemailer' in c:\vhosts\notific
ations-daemons\node_modules\log4js\lib\appenders
 @ ./~/log4js/lib/appenders/smtp.js 4:13-34

The problem is that I don't need all this optional modules in my bundle. The same goes for sequelize where webpack screams that I dont have all the drivers that it supports, even when all I need is mysql driver. Is there any config option or plugin to just ignore all missing dependencies and let the bundle compile without them ?

Upvotes: 5

Views: 6363

Answers (3)

orgads
orgads

Reputation: 696

With webpack 5 and up, the usage of IgnorePlugin has changed. Updated example:

const webpack = require('webpack');
module.exports = {
  plugins: [
    new webpack.IgnorePlugin({ resourceRegExp: /^/u, contextRegExp: /log4js/u })
  ],
};

Or:

const webpack = require('webpack');
module.exports = {
  plugins: [
    new webpack.IgnorePlugin({ resourceRegExp: /^(hipchat-client|loggly|mailgun-js|slack-node|nodemailer)$/u })
  ],
};

Upvotes: 4

Zack W
Zack W

Reputation: 1

I got the same error when I tried to bundle some package that has the dependency on log4j. My webpack version is 4.5.0. Thanks to @Tony Tai Nguyen's answer, I fix the specific error with webpack ignore plugin and add the below config to my webpack.config.js file.

const webpack = require('webpack');
module.exports = {
  plugins: [
    new webpack.IgnorePlugin(/log4j/)
  ],
};

Upvotes: 0

Tony Tai Nguyen
Tony Tai Nguyen

Reputation: 1643

Yup, there's a plugin that can help you with this. You will need to add something like this:

new webpack.IgnorePlugin(new RegExp("/(node_modules|nodemailer)/"))

to your webpack plugins. More information can be found here: https://webpack.github.io/docs/list-of-plugins.html#ignoreplugin

Upvotes: -1

Related Questions