Repository navigation
Closed
Description
Hello I am having problems making lerna and webpack work together in peаce.
[Assumptions]
- Lets imagine we have a lerna-repo with several packages - package1, package2, ... ,packageN.
- Now imagine that package1 depends on package2 and package2 on the other hand depends on another external-package.
- Note that package1 does not depend on external-package since it does not use it (import/require it) in any of its (own) source code.
- Now imagine that package1 needs to build itself to bundle.js via webpack (i believe which bundler is used here does not matter). This might be needed for deployment on a test server or may be just the way the package wants to be distributed, it shouldn't matter.
[The problem]
- As a result of lerna bootstrap in lerna-repo, in lerna-repo/packages/package1/node_modules you will have package2 but you will not have external-package. Note that this will not be true if we forget about lerna for a second and simply npm install package1's dependencies. Hence for some reason the result of lerna bootstrap may be different from the result of npm install for your packages.
Having that in mind you will find webpack failing to bundle your code because of missing dependencies. And in fact they actually are missing in node_modules.
[My assumptions]
- In order to work around this issue we can couple the code in package1 and/or the webpack configuration with the fact that the package is within lerna-repo. However I strongly believe that such coupling is wrong and not intended by the lerna idea. After all this package may tomorrow migrate to some other package managing awesomeness.
- On the other hand the very idea that lerna bootstrap puts an actual link in your packages' node_modules (instead of saving that metadata somewhere else) makes me believe that lerna wants to simulate npm install in your packages, allowing them to not concern that they are currently located in a lerna-repo.
[My suggestion/question]
- Do you find it possible to enhance lerna bootstrap to more precisely simulate npm install and add the correct symlinks for all the packages that npm install would produce? I know it sounds way easier than it actually is, I am asking just about the idea for now.
Metadata
Metadata
Assignees
Labels
No labels
Activity
evocateur commentedon Oct 5, 2017
Do you have an example where this webpack bundling is failing to include a transitive dependency form a lerna-linked (aka "sibling") dependency?
I use webpack to create bundles from lerna packages daily, and I haven't had any issues like this.
Granted, I also explicitly
--hoisteverything to the root of the repo (adding to thedependenciesof the root package.json), so most of mypackages/*/node_modulesdirectories are empty except for relative sibling symlinks. This isn't terribly common, despite being very handy (which I have intended to streamline for awhile, but haven't found the time...)migrachev commentedon Oct 5, 2017
At the moment I cannot give you such and example due to code privacy, but I will come up with one and link it.
migrachev commentedon Oct 8, 2017
After attempting to reproduce the problem with minimum configuration, it turned out that webpack's resolving behavior is configurable and the reason why my described case didn't work was because I specifically say to webpack to resolve package1 node_modules. However when I allowed webpack to respect nested node_modules, I stumbled on another problem, so here is why I will try to give more detailed info here in case someone has similar problem.
[The cause of the problem]
The problem that I specified above was caused by custom webpack configuration. By default webpack should not have problems resolving the node_modules of your node_modules. What I was doing was:
With this configuration webpack will respect only __dirname/node_modules when resolving dependencies and (in the upper mentioned example) dependencies of our dependencies will not be properly resolved. Or the least to say they will be strangely resolved.
This is the configuration that will allow webpack to respect nested node_modules. However in my case that end up in another problem.
[The New Problem]
My packages are using React and React have that restriction that only one instance of React must be loaded by your javascript. The webpack bundle.js was violating that. The reason for that is because my packages depend on React and that is quite normal because they use it. So when webpack transpile and bundle my code, it will load/require React twice.
To solve this problem I use alias for react (I think there are other solutions as well)
With this i think we can close this issue, since webpack is capable of working togerher with lerna.
However I would be interested to read something on the topic i raised about what are the arguments on "Why lerna bootstrap acts with that slight difference from npm install." I guess I am not the first to ask that.
Thanks for the support
evocateur commentedon Oct 8, 2017
lock commentedon Dec 28, 2018
This thread has been automatically locked because there has not been any recent activity after it was closed. Please open a new issue for related bugs.