Skip to content

Bundling with webpack VS Lerna boostraping. #1049

Closed

Description

@migrachev

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.

Activity

  1. evocateur commented on Oct 5, 2017

    @evocateur
    Member

    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 --hoist everything to the root of the repo (adding to the dependencies of the root package.json), so most of my packages/*/node_modules directories 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...)

  2. migrachev commented on Oct 5, 2017

    @migrachev
    Author

    At the moment I cannot give you such and example due to code privacy, but I will come up with one and link it.

  3. migrachev commented on Oct 8, 2017

    @migrachev
    Author

    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:

    resolve: {
            modules: [
                path.join(__dirname, "node_modules")
            ]
    }

    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.

    resolve: {
            modules: [
                "node_modules"
            ]
    }

    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)

    resolve: {
            modules: ["node_modules"],
            alias: {
                react: path.resolve(__dirname, "node_modules/react")
            },
            extensions: [".js", ".css"]
        }

    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

  4. evocateur commented on Oct 8, 2017

    @evocateur
    Member
  5. lock commented on Dec 28, 2018

    @lock

    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.

  6. locked as resolved and limited conversation to collaborators on Dec 28, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions