更改 node_modules 文件夹的位置

Change the location of the node_modules folder

我目前正在尝试将旧的 ASP.NET 网站项目迁移到 Visual Studio 2015。我想使用 NPM/Gulp 自动将 LESS 文件编译为 CSS(此任务由 WebEssentials 在 VS 2013 中完成)。

我在项目中添加了一个package.json文件来加载所需的组件。这会在 WebSite 项目的根目录中创建一个 node_modules 文件夹,这就是我的问题开始的地方:

由于网站项目没有项目文件,项目根文件夹中的所有文件(和子目录)自动成为项目的一部分。由于node_modules内部目录结构嵌套较深,导致路径名过长出错。

一个简单的解决方法是在 node_modules 文件夹上设置 hidden 属性(但这必须由每个开发人员手动完成)。

有没有办法告诉 NPM 将节点模块放入另一个目录,例如项目 (..\node_modules) 上一级解决方案文件在哪里?

或者是否可以通过 gulp 任务(在加载项目时运行)在文件夹上设置隐藏属性?

根据@Rik的回答,我解决了这个问题:

我没有将 package.jsongulpfile.js 添加到网站项目中,而是在解决方案级别(作为解决方案项)添加了它们。这意味着 node_modules 文件夹现在位于与网站项目相同级别的解决方案目录中。

唯一的其他变化是相应地修改 gulpfile.js 中的路径。

您可能想查看 npm 3.0+。它将模块安装在最平坦的结构中。它应该减少模块目录中的路径长度。

来自发行说明

Flat, flat, flat!

Your dependencies will now be installed maximally flat. Insofar as is possible, all of your dependencies, and their dependencies, and THEIR dependencies will be installed in your project's node_modules folder with no nesting. You'll only see modules nested underneath one another when two (or more) modules have conflicting dependencies.

  • #3697 This will hopefully eliminate most cases where windows users ended up with paths that were too long for Explorer and other
    standard tools to deal with.
  • #6912 (#4761 #4037) This also means that your installs will be deduped from the start.
  • #5827 This deduping even extends to git deps.
  • #6936 (#5698) Various commands are dedupe aware now.

This has some implications for the behavior of other commands:

  • npm uninstall removes any dependencies of the module that you specified that aren't required by any other module. Previously, it
    would only remove those that happened to be installed under it,
    resulting in left over cruft if you'd ever deduped.
  • npm ls now shows you your dependency tree organized around what requires what, rather than where those modules are on disk.
  • #6937 npm dedupe now flattens the tree in addition to deduping.

https://github.com/npm/npm/releases/tag/v3.0.0

要升级 windows 安装,请检查这个软件包 npm-windows-upgrade