Repository navigation
Description
Description
The Creating Library guide is totally absent any information on package.json and dependencies.
Currently the library creation guide advocates creating a workspace for every library.
ng new my-workspace --create-application=false
cd my-workspace
ng generate library my-libThis will give you two package.json files, and also two lockfiles
my-workspace/package.jsonmy-workspace/projects/my-lib/package.json
There is no information differentiating these two.
On StackOverflow there is advice to put in dependencies that people have to have to use your library in the my-workspace/projects/my-lib/package.json under the projects directory, and to put the dependencies that you need to develop the library in ./package.json, but this doesn't explicitly address how you can ever have one and not the other in tested functionality.
More succiently, if a user needs a library to run the library in my-workspace/projects/my-lib/package.json won't the developer also need it in my-workspace/package.json so it can be available to ng build, and ng test? Do we have to manually keep these in sync? Is there ever a reason to have a dependency in my-workspace/projects/my-lib/package.json but not in my-workspace/package.json? Is npm install --save not useful for Angular libraries (it only adds to the package.json in the toot of the repo and leaves the files in packages/ out of sync).
My experience with this workspace structure leads me to believe a greater discussion should be had about whether or not this is even good idea. It's very confusing to have reputable tools like @semantic-release (which seem even semi-official) not even support multiple libraries in one workspace when the cli and docs make this sound like a routine practice. It also feels like the cli should be updated to make single-library-per-repo the default, and to restructure this such that there is only ever one package.json and one package in the default configuration.
Activity
TrevorKarjanis commentedon Aug 18, 2021
I had these same questions, and ran into the same SO post. Given the advice on SO and some apparently related issues with library dependencies in application builds, this is my take on the docs section on peer dependencies. Dependencies are touched on but their interpretation can vary based on perspective.
It's assumed this is referring to
workspace/projects/my-lib/package.jsonwhich may not be intuitive if the reader isn't aware that the rootpackage.jsonis for all project dependencies, not just root. There was confusion about this, and some were adding development dependencies toworkspace/projects/my-lib/package.json. Also, what about non-angular dependencies?I assume, but it's not immediately obvious that this is referring to the peer dependencies installed to the workspace of the consuming application which would enforce versions. I think that's because it's not clear whether or not those dependencies were bundled by the library build.
This is confusing, because the documentation states and the CLI places these in
dependenciesnotdevDependencies. The latter half is also confusing, because it's easy to assume that it's referring a library specific set ofnode_modules. Actually, I think it's referring to the workspace which, yes, would be the library's node_modules if you're familiar with what the rootpackage.jsonis for.I am experiencing problems, but I'm not sure if this is the one I'm referring to. Maybe it would be good clarify, because I already expected the library's peer dependencies to be pulled from the application's node_modules. Is there an edge case or different library setup that may or may not apply to me?
[Edit] The first two sentences of Resolving UMD Module Identifiers will probably help a lot of people. However, I haven't seen it mentioned anywhere else in the libraries or ng-packagr docs.
[-]Docs on package.json vs project/foo/package.json[/-][+]Docs on package.json vs project/foo/package.json (and their lockfiles)[/+]EvanCarroll commentedon Apr 21, 2022
@AndrewKushnir what's the process to get this triaged? It would be great to see it added to the backlog.