NPM resolve peer dependencies with transitive dependencies

786 Views Asked by At

I have 3 packages A, B, C, and the i'm developing in requires that package A has B and C as dependecies, but the problem here is that package C also depends on package B, below block describing the case.

enter image description here

So i was thinking of having Package B as peer dependecy inside A,C, but this way it doesn't work, when i tried to run for ex unit test on package A, it always will fail

Start testing for:  /\.main.spec.js?/
module.js:549
    throw err;
    ^

Error: Cannot find module 'B/user/model'
    at Function.Module._resolveFilename (module.js:547:15)
    at Function.Module._load (module.js:474:25)
    at Module.require (module.js:596:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (D:\workspace1\A\node_modules\@comp\C\lib\form\container.js:13:14)
    at Module._compile (module.js:652:30)
    at Module._extensions..js (module.js:663:10)
    at Object.require.extensions.(anonymous function) [as .js] (D:\workspace1\A\node_modules\babel-register\lib\node.js:152:7)
    at Module.load (module.js:565:32)
    at tryModuleLoad (module.js:505:12)
    at Function.Module._load (module.js:497:3)
    at Module.require (module.js:596:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (D:\workspace1\A\node_modules\@comp\C\lib\index.js:12:18)
    at Module._compile (module.js:652:30)
    at Module._extensions..js (module.js:663:10)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! Exit status 1

Package A package.json file

  "dependencies": {
    "@comp/C": "0.0.1"
}
  "peerDependencies": {
    "@comp/B": "0.0.1"
  }

Package C package.json file

  "peerDependencies": {
    "@comp/B": "0.0.1"
  }

Beside that in Package C, i'm not import B functions from the package directly like

import X from '@comp/C'

rather i'm using absolute path for the module like: import X from 'C/X'

and then in webpack i'm using alias for package C,

alias:{
      'C': path.resolve(__dirname, './node_modules/@comp/C/lib/app')
}

for local development it works correctly, but if pushed the packages and tried to use them as a single package (package A) it won't work and will give the above npm error.

I'm i doing it right, or is there another way to handle this case?

0

There are 0 best solutions below