如何在 Windows 上部署具有深层 node_modules 结构的 Node.js 应用程序?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13318364/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 16:36:55  来源:igfitidea点击:

How to deploy Node.js application with deep node_modules structure on Windows?

node.jswindowsdeploymentnpmfilenames

提问by Borek Bernard

I've run into a curious issue - apparently some Node.js module have so deepfolder hierarchiesthat Windows copy command (or PowerShell's Copy-Itemwhich is what we're actually using) hits the infamous "path too long" error when path is over 250 chars long.

我遇到了一个奇怪的问题 - 显然,某些 Node.js 模块的文件夹层次结构如此之Copy-Item以至于当路径超过 250 时,Windows 复制命令(或我们实际使用的PowerShell )会遇到臭名昭著的“路径太长”错误字符长。

For example, this is a folder hierarchy that a single Node module can create:

例如,这是单个 Node 模块可以创建的文件夹层次结构:

node_modules\nodemailer\node_modules\simplesmtp\node_modules\
xoauth2\node_modules\request\node_modules\form-data\node_modules\
combined-stream\node_modules\delayed-stream\...

It seems insane but is a reality with Node modules.

这看起来很疯狂,但在 Node 模块中却是现实。

We need to use copy-paste during deployment (we're not using a "clever" target platform like Heroku where Git deployment would be an option) and this is a serious limitation on Windows.

我们需要在部署期间使用复制粘贴(我们没有使用像 Heroku 这样的“聪明”目标平台,其中 Git 部署将是一个选项),这在 Windows 上是一个严重的限制。

Isn't there a npm command or something that would compact the node_modulesfolder or maybe include only what's actually necessary at runtime?(Node modules usually contain testfolders etc. which we don't need to deploy.) Any other ideas how to work around it? Not using Windows is unfortunately not an option :)

是不是有一个 npm 命令或一些可以压缩node_modules文件夹的东西,或者可能只包含运行时实际需要的东西?(节点模块通常包含test我们不需要部署的文件夹等。)还有其他解决方法的想法吗?不幸的是,不使用 Windows 不是一种选择:)

采纳答案by RameshVel

npm v3(released recently) solves this issue by flattening out the dependencies.. Check the release notes here in https://github.com/npm/npm/releases/tag/v3.0.0under flat flatsection.

npm v3(最近发布)通过扁平化依赖项解决了这个问题。请查看https://github.com/npm/npm/releases/tag/v3.0.0flat flat部分下的发行说明。

And the last comment on this issue https://github.com/npm/npm/issues/3697

以及对这个问题的最后评论https://github.com/npm/npm/issues/3697

回答by Ben Lesh

just to add to this... another thing that helped me was listing out all installed modules with npm ls.

只是为了补充一点......帮助我的另一件事是列出所有已安装的模块npm ls

which will give you a tree of modules and versions... from there it's pretty easy to identify which ones are duplicates... npm dedupedidn't do anything for me. I'm not sure if that's a bug or what (Node v 10.16)

这会给你一个模块和版本树......从那里很容易识别哪些是重复的......npm dedupe对我没有做任何事情。我不确定这是错误还是什么(Node v 10.16)

So once you identify a duplicate module install it to the root node_module directory by using npm install [email protected] --save-dev. The version is important.

因此,一旦您确定重复的模块,请使用 将其安装到根 node_module 目录npm install [email protected] --save-dev版本很重要。

after that, I wiped out my node_modules directory and did a fresh npm install.

在那之后,我删除了我的 node_modules 目录并做了一个新的npm install.

Short version

精简版

  1. npm lsto get a list of all installed modules.
  2. look through those modules and identify duplicate modules (version is important)
  3. npm install module@version --save-devto install those modules in the root node_modules directory and update package.json.
  4. rmdir node_modulesto delete the node_modules directory.
  5. npm installto pull down a fresh copy of your dependencies.
  1. npm ls获取所有已安装模块的列表。
  2. 查看这些模块并识别重复的模块(版本很重要
  3. npm install module@version --save-dev在根 node_modules 目录中安装这些模块并更新 package.json。
  4. rmdir node_modules删除 node_modules 目录。
  5. npm install拉下您的依赖项的新副本。

Once I did that, everything was much cleaner.

一旦我这样做了,一切就干净多了。

I also recommend commenting your package.json file to show which ones were brought down to flatten the node_modules tree.

我还建议对您的 package.json 文件进行评论,以显示哪些文件被降级以扁平化 node_modules 树。

回答by Peter Lyons

I don't think there's any great solution given your constraints, but here are some things that may help.

鉴于您的限制,我认为没有任何好的解决方案,但这里有一些可能会有所帮助的方法。

  • Try using npm dedupeto optimize your directory hierarchy which may shorten some paths
  • Use npm install --productionto install without the development tools
  • Take some of those deeply nested dependencies (just enough to avoid the problem, I suggest) and move them to the top-level node_modules directory. Just keep track of them so you know which are your true dependencies and which are workarounds for this problem.
  • OR move some of those deep dependencies to the highest node_modulesdirectory under your_project/node_modules/pkg_with_deep_depsthat will allow them to have short enough paths but still work. So this would be your_project/node_modules/pkg_with_deep_deps/node_modules.
    • I think requireshould be able to find those properly at run time. You'll just need to clearly document what you have manually changed, why you have done it, and keep your own true dependencies accurately represented in package.json
  • 尝试使用npm dedupe优化您的目录层次结构,这可能会缩短某些路径
  • 用于npm install --production在没有开发工具的情况下安装
  • 获取一些深度嵌套的依赖项(我建议刚好足以避免这个问题)并将它们移动到顶级 node_modules 目录。只需跟踪它们,即可了解哪些是真正的依赖项,哪些是解决此问题的方法。
  • 或者将其中一些深层依赖项移动到最高node_modules目录下your_project/node_modules/pkg_with_deep_deps,这将使它们具有足够短的路径但仍然可以工作。所以这将是your_project/node_modules/pkg_with_deep_deps/node_modules
    • 我认为require应该能够在运行时正确找到那些。您只需要清楚地记录您手动更改的内容、更改的原因,并在package.json

Here is a github issue discussionthat elaborates on this problem in detail.

这里有一个github问题讨论,详细阐述了这个问题。

回答by user3602171

I wrote a node module called "npm-flatten" that flattens your dependencies for you here: https://www.npmjs.org/package/npm-flatten

我在这里写了一个名为“npm-flatten”的节点模块,它为你扁平化你的依赖:https: //www.npmjs.org/package/npm-flatten

If you are looking for a distrubtion, I also wrote a NuGet package that will integrate a complete node.js environment with your .NET project here: http://www.nuget.org/packages/NodeEnv/

如果您正在寻找发行版,我还编写了一个 NuGet 包,它将在此处将完整的 node.js 环境与您的 .NET 项目集成:http://www.nuget.org/packages/NodeEnv/

Feedback would be welcome.

欢迎提供反馈。

回答by David Chelliah

1) During release build, You can prevent Visual studio scanning these files / folder by setting the folder properties as a Hidden folder(JUST set it to node_modules). Reference: http://issues.umbraco.org/issue/U4-6219#comment=67-19103

1) 在发布版本期间,您可以通过将文件夹属性设置为隐藏文件夹(只需将其设置为 node_modules)来防止 Visual Studio 扫描这些文件/文件夹。参考:http: //issues.umbraco.org/issue/U4-6219#comment=67-19103

2) You can exclude files or folder that are published during packaging by including following XML node in the CsProject file.

2) 您可以通过在 CsProject 文件中包含以下 XML 节点来排除打包期间发布的文件或文件夹。

<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
  ...
  <OutputPath>bin\</OutputPath>
   <NoWarn>42016,41999,42017,42018,42019,42032,42036,42020,42021,42022</NoWarn>
  <ExcludeFilesFromDeployment>File1.aspx;File2.aspx</ExcludeFilesFromDeployment>
  <ExcludeFoldersFromDeployment>Folder1;Folder2</ExcludeFoldersFromDeployment>
</PropertyGroup>

回答by zangw

I found one solution from Microsoft Node.js Guidelines.

我从Microsoft Node.js Guidelines找到了一个解决方案。

  • Start in a short path (e.g. c:\src)
  • > npm install -g rimrafdelete files that exceed max_path
  • > npm dedupemoves duplicate packages to top-level
  • > npm install -g flatten-packagesmoves all packages to top-level, but can cause versioning issues
  • Upgrade to npm@3which attempts to the make the node_modulesfolder heirarchy maximally flat.
    • Ships with Node v5
    • Or… > npm install –g npm-windows-upgrade
  • 从短路径开始(例如 c:\src)
  • > npm install -g rimraf删除超过的文件 max_path
  • > npm dedupe将重复的包移动到顶级
  • > npm install -g flatten-packages将所有包移动到顶级,但可能会导致版本控制问题
  • 升级到npm@3哪个尝试使node_modules文件夹层次结构最大程度地平坦。
    • 随 Node v5 一起提供
    • 或者… > npm install –g npm-windows-upgrade

回答by Michael Blankenship

Something that helped me was to map a local drive to my Node.js folder:

帮助我的是将本地驱动器映射到我的 Node.js 文件夹:

net use n: \computername\c$\users\myname\documents\node.js /persistent:yes

净使用 n:\computername\c$\users\myname\documents\node.js /persistent:yes

Before: c:\users\myname\documents\node.js\projectname (45 characters) After: n:\projectname (14 characters which is 31 chars less)

之前:c:\users\myname\documents\node.js\projectname(45 个字符) 之后:n:\projectname(14 个字符,少 31 个字符)

In many cases this allowed some modules to be installed.

在许多情况下,这允许安装一些模块。

I will say that I just re-discovered this problem today when I was attempting to backup all my code to a USB drive.

我要说的是,当我尝试将所有代码备份到 USB 驱动器时,我今天刚刚重新发现了这个问题。

"C:\Users\myname\Documents\Node.js\angular-phonecat\node_modules\karma\node_modules\chokidar\node_modules\anymatch\node_modules\micromatch\node_modules\regex-cache\node_modules\benchmarked\node_modules\file-reader\node_modules\extend-shallow\benchmark\fixtures is too long."

"C:\Users\myname\Documents\Node.js\angular-phonecat\node_modules\karma\node_modules\chokidar\node_modules\anymatch\node_modules\micromatch\node_modules\regex-cache\node_modules\benchmarked\node_modules\file-reader\ node_modules\extend-shallow\benchmark\fixtures 太长了。”

Even when I tried to back them up using the N: drive letter it still failed in some cases due to path lengths but it was just enough to fix the one above.

即使当我尝试使用 N: 驱动器号备份它们时,由于路径长度,它在某些情况下仍然失败,但这足以修复上述问题。

回答by Jason

This is a not a proper solution, rather a work around when you are in a hurry, but you can use 7-Zipto zip your folder, move the zipped file and unzip it without any issue.

这不是一个合适的解决方案,而是在您赶时间时的一种解决方法,但是您可以使用7-Zip压缩文件夹,移动压缩文件并解压缩它,没有任何问题。

We used that solution to deploy a Node.js application where it was not possible to do a clean npm install.

我们使用该解决方案来部署 Node.js 应用程序,而在该应用程序中无法进行干净的 npm 安装。