Answers: <blockquote>
    <p>Strange enough node.js unlikely maven installs the packages the software needs inside the project directory.</p>
  </blockquote>
  
  <p>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: <code>/node_modules</code>, Maven: <code>/target</code>). Otherwise, you cannot continue building the software. (When you run the build system to create the final artifact, it needs the dependencies.)</p>
  
  <p>In addition, both have a global cache to avoid downloading already fetched dependencies: In Maven, the default cache directory is <code>~/.m2</code> (on Linux and MacOS). For npm, it is <code>~/.npm</code>.</p>
  
  <blockquote>
    <p>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?</p>
  </blockquote>
  
  <p>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.</p>
  
  <p>In my previous job, we used <a href="https://www.sonatype.com/nexus-repository-sonatype" rel="noreferrer">Nexus</a> as a caching proxy and local registry (for hosting code that is only accessible within your organization). It supports both npm (see <a href="https://books.sonatype.com/nexus-book/reference/npm.html" rel="noreferrer">documentation</a>) and Maven (see <a href="https://books.sonatype.com/nexus-book/reference/config.html" rel="noreferrer">documentation</a>). (There are other tools, but the point is that both ecosystems can be handled by one tool.)</p>
  
  <p>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 (<a href="https://maven.apache.org/background/philosophy-of-maven.html" rel="noreferrer">"The Maven Way"</a>). But when looking only at the dependency aspect, package managers for Node.js like npm are conceptually not so different from Maven.</p>
  
Answers: CC BY-SA 4.0
Answers: 2019-02-15T15:45:51.057Z
Answers: 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.
Answers: eerriicc
Answers: CC BY-SA 3.0
Answers: 2017-07-19T23:39:47.587Z
Answers: 2017-07-19T23:39:47.587Z
Answers: Philipp Claßen
Answers: <p>In 2022, pnpm:<a href="https://pnpm.io/" rel="nofollow noreferrer">https://pnpm.io/</a> is very similar to maven now. Pnpm will not copy files to node_modules, instead creating hardlink.</p>
  
Answers: CC BY-SA 4.0
Answers: 2022-11-14T09:58:16.953Z
Answers: 2022-11-14T09:59:21.437Z
Answers: 2022-11-14T09:59:21.437Z
Answers: Revenant
Body: <p>Strange enough node.js unlikely maven installs the packages the software needs inside the project directory.</p>
  
  <p>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?</p>
  
Comments: CC BY-SA 3.0
Comments: 2017-06-27T07:39:28.063Z
Comments: 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.
Comments: Evgeny Zislis
Comments: CC BY-SA 3.0
Comments: 2017-06-27T12:32:20.313Z
Comments: 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?
Comments: Ta Mu
Comments: CC BY-SA 3.0
Comments: 2017-06-27T12:53:28.883Z
Comments: `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.
Comments: Evgeny Zislis
Comments: CC BY-SA 3.0
Comments: 2017-06-27T12:53:59.517Z
Comments: 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.
Comments: Evgeny Zislis
Comments: CC BY-SA 4.0
Comments: 2022-12-15T18:54:20.967Z
Comments: Common-features/differences described in detail here: https://stackoverflow.com/a/52745149/1555615
Comments: Marinos An
Tags: <node.js>
Title: How does dependency management for Node.js differ from Maven?
UserDisplayName: Ta Mu
ThreadText: # How does dependency management for Node.js differ from Maven?
  
  (Asked by: username_0 on 2017-06-26)
  
  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?
  
  **Tags:** node.js
  
  > **Comments:**
  > **username_1**: 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.  
  > **username_0**: 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?  
  > **username_1**: `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.  
  > **username_1**: 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.  
  > **username_2**: Common-features/differences described in detail here: https://stackoverflow.com/a/52745149/1555615  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_3 on 2017-07-19. Score: 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](https://www.sonatype.com/nexus-repository-sonatype) as a caching proxy and local registry (for hosting code that is only accessible within your organization). It supports both npm (see [documentation](https://books.sonatype.com/nexus-book/reference/npm.html)) and Maven (see [documentation](https://books.sonatype.com/nexus-book/reference/config.html)). (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"](https://maven.apache.org/background/philosophy-of-maven.html)). But when looking only at the dependency aspect, package managers for Node.js like npm are conceptually not so different from Maven.
  
  > **Comments:**
  > **username_4**: 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.  
  
  ---
  
  ### Answer by username_5 on 2022-11-14. Score: 0
  
  In 2022, pnpm:<https://pnpm.io/> is very similar to maven now. Pnpm will not copy files to node\_modules, instead creating hardlink.
  
  
  ---
  
  