-
-
Notifications
You must be signed in to change notification settings - Fork 8.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unexpected behavior with linked NPM modules #985
Comments
Mhmmm ... we've discussed about that already but I can't find the issue anymore. The problem is that linked modules have another resolved filename and I'm not sure if we can just change that. For instance, node's |
Problem is React uses some global definitions and static objects (such as |
I see the problem, but I don't know how to solve it. webpack implements the node.js resolving algorithm: https://nodejs.org/api/modules.html#modules_all_together So the problem is by design and should occur in node.js too. A workaround is to let webpack prefer the application node_modules with: |
Unfortunately settings
|
I am having this issue too. At some point my changes to my module stopped rebuilding the webpack bit of a project I was integrating it with. |
Hate to bother but is there any update about this issue? |
the workaround explained using |
@jasonslyvia I got around this too that same day. I had to gut the linking and dump every cache and node module I could find. I still don't know if the root of the issue was this code or my fault. |
I have this same issue — the fix that @MoOx specifies works, but I have packages with a bunch of shared dependencies, so it's not a viable long term solution. |
Preferring the application node_modules with For instance - say my app uses moduleA which depends on lodash@3 and moduleB which depends on lodash@4. The npm3 install ends up looking like this:
If I set |
So for this particular problem, there are now two useful solutions (but quite tricky ones): 1.) npm linking the linked module's dependencies to the 2.) sharing a I don't like either of those due to their manual/repetitive/unstable nature. I'd prefer this solution: aurelia/webpack-plugin#44 (comment), a webpack ResolverPlugin that replaces the linked modules node_modules requires with the Update: (thanks @ganmor #985 (comment)) 3.) not using npm link at all for development, but instead using some kind of file syncing tool, i.e. https://github.com/wix/wml or http://www.freefilesync.org/ |
One possible solution is not using npm link at all. |
Uhg this issue is plaguing me right now. The second I link a package in that has a dependency in common with the parent project, my build contains duplicates. |
I've written a resolve plugin which takes care of this issue. What is does is tries to resolve any module calls to the closest one to the provided It's called RootMostResolvePlugin and it's a part of the webpack-dependency-suite (sorry, no docs yet). Configuration is pretty simple (example). Make sure to put the plugin in the When instantiating, the first parameter should be the root directory of your project (or the one in which context you want to resolve) and the second, optional parameter is whether you want to enforce it for all paths (e.g. including linked modules, use |
Hey, just a quick update on our side. We've managed to solve our problems using Using Webpack 2 and setting
So the local/root node_modules is always preferred, and symlinks are not resolved, i.e. same as the node This is working as expected for us, if that helps anyone stumbling upon this issue. |
@jure, you're opening your project up to potential problems if you get many major/minor versions of the same package dependent on a different one, because |
Can someone check this against webpack 2? Thanks. |
@bebraw The problem described in the issue is still present in Webpack 2, and as far as I understand will not be fixed, because the dependency resolution algorithm is based on the one provided by Node. Webpack works properly, but it's an arguably bad design decision on Node's part that Webpack follows. A webpack resolve plugin can be used to mitigate the problem successfully. |
just to provide a bit more insight of what is working, i was able to get this working in webpack 2 with a similar resolve: {
symlinks: false,
modules: [path.resolve('node_modules')],
} it seems like it would pretty safe to default to the local |
Although it would stray from the standard Node.js resolution algorithm, I strongly believe Webpack should be smarter here (at least behind an option) and prefer packages found in the app's @niieani's RootMostResolvePlugin (above) does exactly this. @sokra Do you think it would be worth making this behavior the default in Webpack? |
@niieani I tried
Does it need an update? Does it need to use the EDIT: Oh! It needs to be placed in |
Hey guys, Currently, this is mentioned in https://webpack.js.org/configuration/resolve/#resolve |
Should work with |
@vankop is there a way to get this functionality in webpack@4 as well? |
no, only critical fixes for webpack@4 is possible.. |
@vankop are there any docs about how this is supposed to work in webpack@5 or what are the limitations? cause I'm still running into the same issue - three.js is loaded both from the package's own |
@fabis94 from your description is unclear what is wrong..
maybe some dependency ( or different app modules ) is trying to resolve Probably in your case problem is not related to symlinks.. ( it will work same way with non-symlink version ) you can reduce this to reproducible repo I will take a look. |
I read the description once more.. I thought this related to symlinks.. Issue still exists Basically #985 (comment) is an answer. Since webpack implements Node.js resolve algorithm there is no way to solve this.. |
Just an idea, but why not just compare if the same version of the same package is already bundled in? Or make that an opt-in feature? Like for example, check first The underlying issue is that after linking in modules, npm doesn't deduplicate the node_modules tree so you can end up having But this seemingly npm-specific issue is definitely not going away, considering that symlinking packages into node_modules is a very common workflow. So I think it makes sense for the improvement to be introduced on the webpack side, instead. |
"fix" is possible, but this breaks Node.js resolution algorithm. |
I don't really see why people would want the current behaviour (same package with same version being bundled twice), but it could be an opt-in feature, if you think it's gonna cause issues for some. Anyone trying to deal with these issues and spending hours on it not getting anywhere isn't going to care that the Node.js resolution algorithm is broken, getting dependencies to be properly bundled without duplicates is more important. And of course - by opting in you'd understand what you're doing and the consequences. |
tl;dr
Webpack should produce the same build whether a module is linked or not.
In depth
Considering the following dependency trees:
Notice: NPM does not include
babel-runtime
undersub-project
whennpm list
is called fromproject
.Webpack builds something link:
If we run
npm link sub-project
we obtain the following tree:Notice:
babel-runtime
still doesn't appear undersub-project
.But building the project again will duplicate its dependencies:
Using
resolve.root
or changing the resolve scope of loaders do not change anything.Expectations
It would link to be able the link my in-development modules into my project using
npm link
without having to tweak the webpack config.The real problem isn't the build size but the unexpected behaviors raised but duplicated dependencies.
The text was updated successfully, but these errors were encountered: