简体   繁体   English

nodejs部署的最佳实践 - 将node_modules直接移动到服务器或运行npm install命令

[英]Best practice for nodejs deployment - Directly moving node_modules to server or run npm install command

What is the best practice for deploying a nodejs application? 部署nodejs应用程序的最佳实践是什么?

1) Directly moving the node_modules folders from the development server to production server, so that our same local environment can be created in the production also. 1)直接将node_modules文件夹从开发服务器移动到生产服务器,以便也可以在生产中创建相同的本地环境。 Whatever changes made to any of the node modules remotely will not affect our code. 无论远程对任何节点模块所做的更改都不会影响我们的代码。

2) Run npm install command in the production server with the help of package.json. 2)在package.json的帮助下在生产服务器中运行npm install命令。 Here the problem is, any changes in the node modules will affect our code. 问题在于,节点模块中的任何更改都会影响我们的代码。 I have faced some issues with the loopback module ( issue link ). 我遇到了环回模块的一些问题( 问题链接 )。

Can anyone help me? 谁能帮我?

Running npm install in production server cannot be done in certain scenario (lack of compiling tools, restricted internet access, etc...) and also if you have to deploy the same project on multiple machines, can be a waste of cpu, memory and bandwidth. 在生产服务器中运行npm install无法在某些情况下完成(缺少编译工具,限制互联网访问等等),如果你必须在多台机器上部署同一个项目,可能会浪费cpu,内存和带宽。

You should run npm install --production on a machine with the same libraries and node version of the production server, compress node_modules and deploy on production server. 您应该在具有相同库和生产服务器节点版本的计算机上运行npm install --production ,压缩node_modules并在生产服务器上部署。 You should also keep the package-lock.json file to pinpoint versions. 您还应该保留package-lock.json文件以精确定位版本。

This approach allows you also to build/test your code using development packages and then pruning the node_modules before the actual deploy. 此方法还允许您使用开发包构建/测试代码,然后在实际部署之前修剪node_modules。

My guess is that by asking this question you don't really understand the point of the package.json file. 我的猜测是,通过提出这个问题,你并不真正理解package.json文件的重点。

The package.json file is explicitly intended for this purpose (that, and uploading to the npm registry), the transfer of a node package without having to transfer the sizeable number of dependencies along with it. package.json文件明确用于此目的(并上传到npm注册表),传输节点包而不必随之传输大量的依赖项。

I would go as far as to say that one should never manually move the node_modules directory at all. 我甚至会说, 根本不应该手动移动node_modules目录。

Definitely use the npm install command on your production server, this is the proper way of doing it. 绝对在生产服务器上使用npm install命令,这是正确的方法。 To avoid any changes to the node_modules directory as compared to your local environment, use the package lock file. 要避免与本地环境相比对node_modules目录进行任何更改,请使用package lock文件。 That should help with minimising changes to the source code in node_modules . 这应该有助于最小化node_modules源代码的node_modules

I mean no bad intent by saying this 我的意思是说这不是坏事

Definitely npm install . 绝对是npm install But you shouldn't do this by your own hand when it comes to deploying your app. 但是,在部署应用程序时,您不应该自己动手做。

Use the tool for this like PM2 . PM2一样使用此工具。

As for your concern about changes in packages, the short answer is package-lock.json . 至于你对包中更改的关注,简短的回答是package-lock.json

  • Moving node_modules folder is overkilled. 移动node_modules文件夹过度。
  • Running npm install might break the version dependencies. 运行npm install可能会破坏版本依赖性。
  • The best approach is npm ci . 最好的方法是npm ci It uses the package_lock file and installs the required dependencies without modify the versions. 它使用package_lock文件并安装所需的依赖项而不修改版本。 npm ci meant for continuous integration projects. npm ci意味着持续集成项目。 LINK 链接

I am an ASP.NET Core developer but I recently started working with Node.js apps. 我是一名ASP.NET核心开发人员,但我最近开始使用Node.js应用程序。 For me this was one of the challenges you mentioned to move the node_modules folder to production. 对我来说,这是你提到的将node_modules文件夹移动到生产的挑战之一。 Instead of moving the whole folder to production or only running the npm install command on production server, I figured out and tried a way of bundling my Node.js app using Webpack into a single/multiple bundles, and I just got rid of the mess of managing node_modules folder. 我没有将整个文件夹移动到生产或只在生产服务器上运行npm install命令,而是想出了一种方法,将我的Node.js应用程序使用Webpack捆绑成一个/多个捆绑包,我只是摆脱了混乱管理node_modules文件夹。 It only picks up the required node_modules packages that are being used/referred in my app and bundles up in a single file along with my app code and I deploy that single file to production without moving the entire node_modules folder. 它只获取在我的应用程序中使用/引用的所需node_modules包,并将其与我的应用程序代码捆绑在一个文件中,并将该单个文件部署到生产中,而无需移动整个node_modules文件夹。

I found this approach useful in my case but please suggest me if this is not the correct way regarding the performance of the app or if any cons of this approach. 我发现这种方法在我的情况下很有用,但如果这不是关于应用程序性能的正确方法或者这种方法的任何缺点,请建议我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM