9

Strange enough node.js unlikely maven installs the packages the software needs inside the project directory.

Is there a way to introduce a central repository which also caches different versions of components and would allow to pull them on build like we know it from other ecosystems?

5
  • 2
    The yarn replacement for npm from Facebook does have a global per-user cache just like Maven does. The node_modules folder is not a cache, it is used by the software as its dependencies store. Jun 27, 2017 at 7:39
  • just for curioisity, why wouldn't you place this as answer? (I would accept it). And, wouldn't npm -g allow for global package management as well, Maven-like?
    – Ta Mu
    Jun 27, 2017 at 12:32
  • npm -g would "install" packages in the global namespace, this is not the same as caching. Caching has all the various versions of each specific package, and when you need to install that specific version - it would use the cache instead of downloading the source over the internet. Actually installing the package globally doesn't have this property. Jun 27, 2017 at 12:53
  • Didn't add this as the answer, since the question is very unclear to me. I don't understand exactly what the author wants to know, what you ask. Jun 27, 2017 at 12:53
  • Common-features/differences described in detail here: stackoverflow.com/a/52745149/1555615
    – Marinos An
    Dec 15, 2022 at 18:54

2 Answers 2

6

Strange enough node.js unlikely maven installs the packages the software needs inside the project directory.

I don't see fundamental differences between npm on node.js and Maven. In both cases, the dependencies finally end up in the project directory (npm: /node_modules, Maven: /target). Otherwise, you cannot continue building the software. (When you run the build system to create the final artifact, it needs the dependencies.)

In addition, both have a global cache to avoid downloading already fetched dependencies: In Maven, the default cache directory is ~/.m2 (on Linux and MacOS). For npm, it is ~/.npm.

Is there a way to introduce a central repository which also caches different versions of components and would allow to pull them on build like we know it from other ecosystems?

If the system cache is not enough and you do not want to depend completely on the public repository (npm: registry.npmjs.org, Maven: Maven Central), you can run your own mirror. For instance, if you want to be able to rebuild the software even if the public repository is down.

In my previous job, we used Nexus as a caching proxy and local registry (for hosting code that is only accessible within your organization). It supports both npm (see documentation) and Maven (see documentation). (There are other tools, but the point is that both ecosystems can be handled by one tool.)

Of course, there are differences between Maven and npm. For instance, Maven is also a full build system, whereas the scope of npm is primarily dependency management. Maven also defines conventions to structure a Java project ("The Maven Way"). But when looking only at the dependency aspect, package managers for Node.js like npm are conceptually not so different from Maven.

1
  • 5
    Just a small correction: Maven doesn't copy the dependencies to the target folder. It builds its classpath from the cached artifacts in the local Maven repository.
    – eerriicc
    Feb 15, 2019 at 15:45
0

In 2022, pnpm:https://pnpm.io/ is very similar to maven now. Pnpm will not copy files to node_modules, instead creating hardlink.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.