You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Suppose we are making a package that we want to publish to npm.
My understanding of dependencies, devDependencies, and peerDependencies is as follows:
dependencies are packages that we do not want the people who download our package to ever really have to think about, so we bundle them into our dist file. We do not put them into our bundler's "externals" config.
devDependencies are things like unit test runners, eslint, and rollup itself. We should never be importing them into our source code, and therefore it's irrelevant whether we include them into "externals" or not.
peerDependencies are packages which we want the people who download our package to ALSO install. Therefore we put them into the "externals" config to prevent duplication between our bundle and the downloaders' installations.
This understanding does not seem to match with the defaults of this plugin. My understanding is that the defaults should be:
deps: false
devDeps: false
peerDeps: true
Can someone explain what is different between my understanding and this plugin's philosophy?
You've got it partly right -- and hence, partly wrong.
dependencies are packages that are needed to run/use your package at run time. They are not bundled with your package (meaning, they are not physically part of it); instead, npm will automatically download and install them alongside your package when a user runs npm install <your-package>. They should always be external because if Rollup bundled them there would be code duplication between the code of your package and the user's installation.
devDependencies are support packages that are needed to build your package. As you said, those include things like test runners, linters, and Rollup itself. But if you ask Rollup to bundle some package inside you code, then that package is a development dependency also (it is needed to build your package but not to run/use it since it is bundled). devDependencies should not be external as otherwise Rollup would not bundle them and your package would simply not work at run time. And of course, devDependencies are only bundled if your code actually requires/imports them.
peerDependencies are very similar to dependencies, but the other way around: they do not express packages that are needed by your package but packages that can require/import yours.
There are many blog posts and tutorials on the Web that may give you a better understanding, like this one or that one.
Therefore, this plugin's defaults are exactly right: only devDependencies are bundled (not external), all other kinds of dependencies are external (not bundled).
I think we are agreed with regards to devDependencies and peerDependencies. The following defaults are correct:
devDeps: false
peerDeps: true
optDeps: true
I want to push a little more on dependencies.
You say that:
npm will automatically download and install them alongside your package when a user runs npm install
I agree with this statement. However, I worry that this is dependent on npm's flattened node_modules structure. Because both the dependencies and transitive dependencies of a project are located as siblings within the node_modules directory, a project is able to require/import from a transitive dependency without error.
Flattened node_modules looks like the following, where dependency-a depends on dependency-b:
node_modules/
- dependency-a/
- dependency-b/
If instead you are using pnpm, the node_modules directory is NOT flattened (similar to early npm) and instead looks like:
This discussion is going far beyond the scope of Rollup and my plugin but in a nutshell: both Node's CommonJS and ESM resolution algorithms work regardless of the node_modules folder being flattened or not1. Choosing whether a dependency should be bundled by Rollup or dynamically loaded at runtime is therefore irrelevant to the node_modules structure; instead other considerations come into play such as whether you are building an app or a library, targeting the browser or the NodeJS runtime, bootstrap performance, ease of dependencies update etc.
Footnotes
this does affect transitive dependencies imports, though. As a rule of thumb, you should never require/import dependencies that are not explicitly declared in your package.json. ↩
Activity
Septh commentedon Mar 21, 2024
You've got it partly right -- and hence, partly wrong.
dependenciesare packages that are needed to run/use your package at run time. They are not bundled with your package (meaning, they are not physically part of it); instead,npmwill automatically download and install them alongside your package when a user runsnpm install <your-package>.They should always be
externalbecause if Rollup bundled them there would be code duplication between the code of your package and the user's installation.devDependenciesare support packages that are needed to build your package. As you said, those include things like test runners, linters, and Rollup itself. But if you ask Rollup to bundle some package inside you code, then that package is a development dependency also (it is needed to build your package but not to run/use it since it is bundled).devDependenciesshould not beexternalas otherwise Rollup would not bundle them and your package would simply not work at run time.And of course,
devDependenciesare only bundled if your code actually requires/imports them.peerDependenciesare very similar todependencies, but the other way around: they do not express packages that are needed by your package but packages that can require/import yours.There are many blog posts and tutorials on the Web that may give you a better understanding, like this one or that one.
Therefore, this plugin's defaults are exactly right: only
devDependenciesare bundled (not external), all other kinds of dependencies are external (not bundled).onlywei commentedon Mar 21, 2024
I think we are agreed with regards to
devDependenciesandpeerDependencies. The following defaults are correct:I want to push a little more on
dependencies.You say that:
I agree with this statement. However, I worry that this is dependent on npm's flattened
node_modulesstructure. Because both the dependencies and transitive dependencies of a project are located as siblings within thenode_modulesdirectory, a project is able to require/import from a transitive dependency without error.Flattened
node_moduleslooks like the following, wheredependency-adepends ondependency-b:If instead you are using
pnpm, thenode_modulesdirectory is NOT flattened (similar to early npm) and instead looks like:Are you sure the runtime will still be able to pick up
dependency-bif it's not bundled in that kind ofnode_modulesstructure?Septh commentedon Mar 22, 2024
This discussion is going far beyond the scope of Rollup and my plugin but in a nutshell: both Node's CommonJS and ESM resolution algorithms work regardless of the
node_modulesfolder being flattened or not1. Choosing whether a dependency should be bundled by Rollup or dynamically loaded at runtime is therefore irrelevant to thenode_modulesstructure; instead other considerations come into play such as whether you are building an app or a library, targeting the browser or the NodeJS runtime, bootstrap performance, ease of dependencies update etc.Footnotes
this does affect transitive dependencies imports, though. As a rule of thumb, you should never require/import dependencies that are not explicitly declared in your
package.json. ↩onlywei commentedon Mar 22, 2024
Thanks for the response! I understand now!