npm 5.8.0
ProjectA and a ProjectB
ProjectB is a dependency of ProjectA
For development purposes I am referencing ProjectB in ProjectA's package.json via "projectB": "file:../projectB". Basically the only files I need there are in the lib folder which is Webpack's output.
If I install this dependecy with the configuration above, npm will install the entire ProjectB-folder into node_modules. It will contain everything projectB's node_modules, configuration files, the src-folder etc.
Apart from the fact that this can't be the expected behaviour, this leads to errors. For example some of the installed @types will throw an error because they are considered duplicates. @types and other packages from ProjectA and ProjectB seem to "collide". Those packages are referenced as "dependencies" in both ProjectA and ProjectB mostly.
If I use npm link the same (see above) behaviour appears. The entire folder will be installed into ProjectA's node_modules.
It might be important that I actually do have a .npmignore-file in ProjectB. So when I use npm pack a projectB.tgz file is generated. If I then install ProjectB via "projectB": "file:../projectB.tgz" everything works fine. I assume that npm pack does take the .npmignore-file into account.
My problem with this solution is that I not only would have to build ProjectB every time a change is applied to it but also npm pack it.
ProjectB's node_modulesI guess this is the silliest workaround. If I reference ProjectB via "projectB": "file:../projectB" again but delete its node_modules after building it, they do not appear after installation. Thus I do not get any more exceptions.
I guess this is not a valid solution either as still the entire ProjectB-folder is being installed.
What is the best practice here? What is a reasonable constallation for ProjectA and ProjectB in order to install ProjectB from a local source?
If projectA requires files from projectB/lib, you could configure webpack's resolver as follows (assuming webpack 4):
resolve {
alias: "projectB": "projectB/lib"
}
If projectB is a module, it's package.json should include the corresponding entrypoint (see this page).
I would also suggest you try yarn instead of npm for package management. Yarn's workspaces feature is designed for working with multiple interdependent packages.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With