terence55 / themes-switch

Toolset for switch multiple themes in application based on webpack
54 stars 18 forks source link

Error: Cannot find module 'html-webpack-plugin' #14

Closed wmcmurray closed 2 years ago

wmcmurray commented 3 years ago

I tried to use this plugin in a project that does not use html-webpack-plugin at all, and I get this error in webpack because html-webpack-plugin is required, and since it's not listed in the dependencies, it does not get installed by npm.

Here is where it is required and used :

https://github.com/terence55/themes-switch/blob/f595ce091e0eb54508028782797f66623f3c39b9/src/ThemesGeneratorPlugin.js#L6

https://github.com/terence55/themes-switch/blob/f595ce091e0eb54508028782797f66623f3c39b9/src/ThemesGeneratorPlugin.js#L127

I saw it being listed in the peerDependencies, so I poked around the npm documentation and found this code exemple that might show a better way of handling such dependency :

try {
  var foo = require('foo')
} catch (er) {
  foo = null
}

// .. then later in your program ..

if (foo) {
  foo.doFooThings()
}

Thank you !

terence55 commented 3 years ago

Thank you for your suggestion. But it seems that your sample is for optionalDependencies but not for peerDependencies, and optionalDependencies means the library could be optional, and the project can work without it, but peerDependencies means that is a required option. You will receive a warning if the peerDependency is not installed when running npm install or yarn. I think placing require in the top level of a module can make it easier to identify dependencies, so usually I won’t put it in statements unless really necessary. Here are some links for reference: https://nodejs.org/en/blog/npm/peer-dependencies/ https://eslint.org/docs/rules/global-require