When attempting to utilize npm's peerDependencies
, I'm encountering issues that do not align with the expected behavior. What could be causing this discrepancy?
The scenario is as follows: I have two modules, mod
and plugin
, both of which rely on an external module from npm. mod
specifies a firm dependency on both plugin
and the external module, while the plugin defines a peer dependency to access the version utilized by the parent module.
The file structure appears like this:
~/plugin/package.json:
{
"name": "plugin",
"version": "1.0.0",
"main": "index.js",
"peerDependencies": {
"pad-right": "^0.2.2"
}
}
~/plugin/index.js
var peerDependency = require('pad-right')
module.exports = 'this is a plugin'
~/mod/package.json:
{
"name": "mod",
"version": "1.0.0",
"main": "index.js",
"dependencies": {
"pad-right": "^0.2.2",
"plugin": "../plugin"
}
}
~/mod/index.js:
var hardDependency = require('pad-right')
var plugin = require('plugin')
Based on documentation and examples, it appears that this method should be standard practice for using peer dependencies. Executing npm install
in either directory does not yield any errors or warnings.
However, when running webpack within the mod
directory, errors such as the following arise:
ERROR in ../plugin/index.js
Module not found: Error: Can't resolve 'pad-right' in '~/plugin'
@ ../plugin/index.js 1:21-41
@ ./index.js
This raises the question - why is webpack unable to resolve the require statement within plugin
using the peer dependency specified in the parent mod
module?