node.js 节点 npm windows 文件路径太长无法安装包
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26155135/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Node npm windows file paths are too long to install packages
提问by Allan McLemore
Situation
情况
I want to use gulp and related front-end tool chains in Windows-hosted development environments. I'm hitting a wall trying to use gulp plug-ins like Browser-Sync, because the node_modules folder graph fans out making the windows file paths too long to copy the files. I'd like a pragmatic approach for handling this problem right now on Windows, irrespective of what the Node community may or may not provide to improve npm usability on the Windows in the future.
我想在 Windows 托管的开发环境中使用 gulp 和相关的前端工具链。我在尝试使用像 Browser-Sync 这样的 gulp 插件时碰壁了,因为 node_modules 文件夹图扇出使 windows 文件路径太长而无法复制文件。我想要一种务实的方法来立即在 Windows 上处理这个问题,而不管 Node 社区将来可能会或不会提供什么来提高 Windows 上的 npm 可用性。
2 Questions
2 问题
Is there an npm workflow for Windows that just works the way it was intended? "run the command and the files install" (e.g. comparable to npm on OSX, npm on Linux, ruby gems or even nuget) I don't want to fiddle with a bunch of manual file edits, symlinks, etc. every time I use npm on Windows.
Is there a well-documented, stable Cygwin workflow for npm and node execution to workaround the Windows API file path limits?
是否有适用于 Windows 的 npm 工作流程可以按照预期的方式工作?“运行命令并安装文件”(例如,与 OSX 上的 npm、Linux 上的 npm、ruby gems 甚至 nuget 相当)我不想每次使用时都摆弄一堆手动文件编辑、符号链接等Windows 上的 npm。
是否有用于 npm 和节点执行的记录良好且稳定的 Cygwin 工作流程来解决 Windows API 文件路径限制?
Gory details listed below...
下面列出了血腥的细节......
General Problem
一般问题
- Running npm install from a standard Windows command prompt fails on deeply nested node_modules hierarchies.
- Per Joyent's github repo thread, this is an acknowledged issuewith no palatable workarounds for developers in Windows-centric environments. (Really?)
- NT Kernel supports file path lengths up to 32,767 characters.
- Windows API's MAXPATH is limited to 260 characters.
- Windows API handles file operations for all major Windows shells and whatnot including: Explorer, CMD, Powershell,MYSgit bash, etc. (MS really? How long has NTFS been around?)
- Cygwin supports long file paths, but npm.cmd doesn't work out-of-box due to crlf formatting. I tried the DOS2Unix transform on npm to get it working with Cygwin, but there seem to be other issues with this.
- 在深度嵌套的 node_modules 层次结构上,从标准 Windows 命令提示符运行 npm install 失败。
- 根据 Joyent 的 github repo 线程,这是一个公认的问题,对于以 Windows 为中心的环境中的开发人员来说,没有合适的解决方法。(真的吗?)
- NT 内核支持最多 32,767 个字符的文件路径长度。
- Windows API 的 MAXPATH 限制为 260 个字符。
- Windows API 处理所有主要 Windows shell 的文件操作,包括:资源管理器、CMD、Powershell、MYSgit bash 等(真的是 MS?NTFS 已经存在多久了?)
- Cygwin 支持长文件路径,但 npm.cmd 由于 crlf 格式而无法开箱即用。我在 npm 上尝试了 DOS2Unix 转换以使其与 Cygwin 一起使用,但似乎还有其他问题。
My Current Hack
我目前的黑客
- Create an "n" folder as a staging area on the root of C:\, because this shortens my folder path.
- Run npm inside the "n" folder to install modules for whatever I need.
- Fire up Cygwin and use cp to copy the node_modules folder into a destination project.
- Rinse and repeat when dependencies change or when I need to spin up a new project.
- 在 C:\ 的根目录上创建一个“n”文件夹作为暂存区,因为这会缩短我的文件夹路径。
- 在“n”文件夹中运行 npm 以安装我需要的任何模块。
- 启动 Cygwin 并使用 cp 将 node_modules 文件夹复制到目标项目中。
- 当依赖关系发生变化或我需要启动一个新项目时,冲洗并重复。
Other Unpalatable Workarounds
其他令人不快的解决方法
Symbolic Linkscan be used to shorten file paths, but these are kludgy hacks. As the npm ecosystem grows, nested dependency chains will become too long and this workaround become unusable.
符号链接可用于缩短文件路径,但这些都是笨拙的技巧。随着 npm 生态系统的发展,嵌套的依赖链将变得太长,这种变通方法变得不可用。
Adding ALL dependences to the root folder's package.jsonfile was mentioned in one thread I came across. Although this approach will flatten the folder structure and prevent loading of duplicate modules, this workaround feels unnatural. It also kills the usability, durability, and productivity of npm, because you have to fiddle with files and folders post-install either manually or with some hacky scripts. The approach is also vulnerable to the same fate that Symbolic Links approach may eventually suffer.
在我遇到的一个线程中提到了将所有依赖项添加到根文件夹的 package.json文件中。虽然这种方法会扁平化文件夹结构并防止加载重复的模块,但这种解决方法感觉不自然。它还破坏了 npm 的可用性、耐用性和生产力,因为您必须手动或使用一些 hacky 脚本在安装后摆弄文件和文件夹。这种方法也容易受到符号链接方法最终可能遭受的同样命运的影响。
回答by biofractal
The problem with deeply nested folders on Windows has been mostly solved starting with npm version 3.x.
从 npm version 开始,Windows 上深度嵌套文件夹的问题已基本得到解决3.x。
According to npm:
根据 npm:
.npm@3 makes the install "maximally flat" by hoisting everything it can to the top level node_modules. This means nesting only occurs on conflicts and as such, trees should never get very deep. As such, the windows path length limitation shouldn't be run into.
.npm@3 通过将它所能做的一切提升到顶级 node_modules 来使安装“最大程度地平坦”。这意味着嵌套只发生在冲突中,因此,树永远不会变得很深。因此,不应遇到 Windows 路径长度限制。
I have just installed npm 3.1.0and tried it out on a package that was throwing the dreaded The specified path, file name, or both are too longerror.
我刚刚安装了 npm3.1.0并在一个抛出可怕The specified path, file name, or both are too long错误的包上进行了尝试。
The problem went away.
问题就解决了。
You can get the latest npm builds from here : npm releases
您可以从这里获取最新的 npm 版本:npm 版本
回答by Marcelo Mason
Windows 8.1 and 10 have an option to increase the Win32 path limit:
Windows 8.1 和 10 可以选择增加 Win32 路径限制:
- Open Group Policy Editor (Press Windows+Rand type
gpedit.mscand hit Enter) - Navigate to the following directory:
Local Computer Policy\Computer Configuration\Administrative Templates\System\Filesystem - Doubleclick on Enable Win32 long pathsoption and enable it.
- 打开组策略编辑器(按Windows+R并键入
gpedit.msc并点击Enter) - 导航到以下目录:
Local Computer Policy\Computer Configuration\Administrative Templates\System\Filesystem - 双击启用 Win32 长路径选项并启用它。
回答by Amol M Kulkarni
This is a work around solution.
这是一个变通的解决方案。
There are some node modules that flattens your dependencies for you.
Links are here:
有一些节点模块可以为您扁平化您的依赖项。
链接在这里:
What these modules are doing can be done manually as well. This is the only real solution exists as of now, i.e to have all your modules at a single level, requiring each other, instead of all having private copies of their dependencies nested deeply.
这些模块正在做的事情也可以手动完成。这是目前唯一真正存在的解决方案,即让所有模块都在一个级别上,相互要求,而不是所有模块都具有深度嵌套的依赖项的私有副本。
回答by Sam Mikes
Allan -
艾伦——
From the github issue you linked,
从您链接的github问题,
npm will add dedupe-at-install-time by default. This is significantly more feasible than Node's module system changing, but it is still not exactly trivial, and involves a lot of reworking of some long-entrenched patterns.
npm 默认会添加 dedupe-at-install-time。这比 Node 的模块系统更改明显更可行,但它仍然不是微不足道的,并且涉及对一些根深蒂固的模式进行大量修改。
This is (finally) currently in the works at npm, going by the name multi-stage-install, and is targeted for npm@3. npmdevelopment lead Forrest Norvell is going to spend some time running on Windows in the new year, so please do create windows-related issues on the npmissue tracker < https://github.com/npm/npm/issues>
这(最终)目前正在 npm 中进行,名称为multi-stage-install,并且针对npm@3. npm开发负责人 Forrest Norvell 将在新的一年里花一些时间在 Windows 上运行,所以请在npm问题跟踪器上创建与 Windows 相关的问题 < https://github.com/npm/npm/issues>
回答by Stefan Mohr
I have the same issue. Flattening the dependencies isn't a complete solution, since you might be using modules that depend on different versions of the same dependent module. I discovered the gulp-run module stopped working after flattening (related to module assumptions about bin/.bin directories, I suspect). Drat!
我有同样的问题。扁平化依赖项并不是一个完整的解决方案,因为您可能正在使用依赖于同一依赖模块的不同版本的模块。我发现 gulp-run 模块在展平后停止工作(我怀疑与关于 bin/.bin 目录的模块假设有关)。天啊!
There's lots of discussion about the problem, but no solution in sight: https://github.com/joyent/node/issues/6960
关于这个问题有很多讨论,但看不到解决方案:https: //github.com/joyent/node/issues/6960
https://github.com/npm/npm/issues/3697
https://github.com/npm/npm/issues/3697
A workaround that's working for me is to manually add dependencies my project doesn't explicitly need.
对我有用的解决方法是手动添加我的项目并不明确需要的依赖项。
If you want to identify which packages are giving you problems, I found PathLengthCheckerquite useful. Just extract the EXE and run the GUI or command line app. The other way I've uncovered the problem is to try to build in Visual Studio, but it fails without telling you whichdirectory name is too long.
如果您想确定哪些包给您带来了问题,我发现PathLengthChecker非常有用。只需提取 EXE 并运行 GUI 或命令行应用程序。我发现问题的另一种方法是尝试在 Visual Studio 中构建,但它失败了,但没有告诉您哪个目录名太长。
Here's a command line example of my workaround:
这是我的解决方法的命令行示例:
mkdir c:\reallylongdirectorywillbreakinwindows
cd c:\reallylongdirectorywillbreakinwindows
npm init
npm install --save-dev grunt-bower-task
PathLengthChecker.exe RootDirectory="C:\reallylongdirectorywillbreakinwindows" MinLength=260
I got back:
我回来了:
261: C:\reallylongdirectorywillbreakinwindows\node_modules\grunt-bower-task\node_modules\bower\node_modules\update-notifier\node_modules\latest-version\node_modules\package-json\no de_modules\registry-url\node_modules\npmconf\node_modules\config-chain\readme.markdown
[snip - there were 12 of them]
261:C:\reallylongdirectorywillbreakinwindows\node_modules\grunt-bower-task\node_modules\bower\node_modules\update-notifier\node_modules\latest-version\node_modules\package-json\no de_modules\registry-url\node_modules\npmconf\node_modules\配置链\readme.markdown
[剪断 - 其中有 12 个]
According to the npm lscommand:
根据npm ls命令:
└─┬ [email protected]
├── [email protected]
├─┬ [email protected]
│ ├─┬ [email protected]
│ │ ├─┬ [email protected]
│ │ │ └─┬ [email protected]
│ │ │ └─┬ [email protected]
│ │ │ └─┬ [email protected]
│ │ │ ├─┬ [email protected]
│ │ │ │ └── [email protected]
Let's go with npmconf - it's the container for all the over-length files that are causing issues. We need npmconf 2.1.1.
让我们使用 npmconf - 它是所有导致问题的超长文件的容器。我们需要 npmconf 2.1.1。
npm install --save-dev [email protected]
(now delete the node_modules directory - you may have to use Windows Explorer if you can't do it with rmdir /s)
npm install
PathLengthChecker.exe RootDirectory="C:\reallylongdirectorywillbreakinwindows" MinLength=260
No results - all files are within limits!
没有结果 - 所有文件都在限制范围内!
The obvious caveat here is that it only works once per package - dependencies on different versions of the same module can't be installed at the root node_modules level because node doesn't account for versions in the directory structure.
这里明显的警告是它每个包只能工作一次 - 不能在根 node_modules 级别安装对同一模块不同版本的依赖,因为 node 不考虑目录结构中的版本。
This workaround isn't perfect, but it solves my main goals of having node work on Windows, and since the resolution is right in package.json, the workaround works for other developers and build servers without any manual or global fussing.
这种解决方法并不完美,但它解决了我在 Windows 上运行节点的主要目标,并且由于解决方案在 package.json 中是正确的,因此该解决方法适用于其他开发人员并构建服务器而无需任何手动或全局大惊小怪。
回答by gwildu
If you are OK with installing it globally this could be a work-around:
如果您可以全局安装它,这可能是一种解决方法:
You can adjust the path where npm is installing the global modules to something very short (usually it is: c:\users\\{username}\AppData\Roaming\npm\npm_modules) which already takes a lot of characters.
您可以将 npm 安装全局模块的路径调整为非常短的路径(通常是 : c:\users\\{username}\AppData\Roaming\npm\npm_modules),这已经占用了很多字符。
To adjust it see here: Change default global installation directory for node.js modules in Windows?
要调整它,请参见此处:更改 Windows 中 node.js 模块的默认全局安装目录?
If you adjust it to, e.g., c:\n\in some cases it might solve the issue.
如果你调整它,例如,c:\n\在某些情况下它可能会解决问题。
回答by user3310182
This is what finally fixed it for me...
这就是最终为我修复的...
After installing gulp and receiving errors, run... gulp
安装 gulp 并收到错误后,运行... gulp
When you see a package failing, install it manually with --no-bin-link.
当您看到软件包失败时,请使用--no-bin-link.
sudo npm install {package} --no-bin-link
Where {package} is the package that is having problems.
其中 {package} 是有问题的包。
After all of this I was receiving an Error in plugin 'gulp-notify' Message: not found: notify-send.
在所有这些之后,我在插件“gulp-notify”消息中收到错误消息:未找到:通知发送。
This was due to a plugin issue with Vagrant. You can either turn off notifications..
这是由于 Vagrant 的插件问题。您可以关闭通知..
export DISABLE_NOTIFIER=true;
Or install the plugin with Vagrant.
或者使用Vagrant安装插件。
Best of luck.. I spent a long time on this, even after following a lot of people's recommendations.
祝你好运.. 我在这上面花了很长时间,即使是在遵循了很多人的建议之后。
Brandon
布兰登
回答by Justis Matotoka
In windows:
在窗口中:
- Using your windows explorer, Navigate to your vagrant shared folder (I am using scotchbox by the way) e.g
C:\scotchbox/public/gulpProject - In the address bar of the folder, type
cmdand press Enter - Do your gulp installation
npm install
- 使用您的 Windows 资源管理器,导航到您的 vagrant 共享文件夹(顺便说一下,我正在使用 scotchbox)例如
C:\scotchbox/public/gulpProject - 在文件夹的地址栏中,键入
cmd并按Enter - 做你的 gulp 安装
npm install
回答by kenberkeley
npm install --no-bin-link. You will have a entire flattenednode_modules
npm install --no-bin-link. 你将有一个完整的扁平化node_modules


