简体   繁体   English

在没有 docker 的情况下在生产中部署 TypeScript Node.js 应用程序

[英]Deploy TypeScript Node.js application in production without docker

What is the current best practice to deploy TypeScript Node.js applications in production without docker?在没有 docker 的情况下在生产中部署 TypeScript Node.js 应用程序的当前最佳实践是什么?

In case of using git there is the problem that you need to install all dependencies in order to build the project since the types are required which are usually installed as devDependencies.在使用 git 的情况下,存在的问题是您需要安装所有依赖项才能构建项目,因为通常需要安装为 devDependencies 的类型。 This also means that you install modules for testing, linting, etc. which are not required at all in production.这也意味着您安装了用于测试、linting 等的模块,而这些在生产中根本不需要。

I am thinking about adding all types as dependencies which then would allow to run npm install --production in order to avoid installing all devDependencies and still be able to build the project by running npm run build .我正在考虑将所有类型添加为依赖项,然后允许运行npm install --production以避免安装所有 devDependencies 并且仍然能够通过运行npm run build来构建项目。

Another solution I was thinking about was deployment via npm, similiar to how TypeScript node modules are released and installed but that solution also seems suboptimal.我正在考虑的另一个解决方案是通过 npm 进行部署,类似于 TypeScript 节点模块的发布和安装方式,但该解决方案似乎也不是最理想的。

Why would you add all types as dependencies?为什么要将所有类型添加为依赖项? There is no need for that, your production app shouldn't contain all of your dev dependencies.没有必要,您的生产应用程序不应包含所有开发依赖项。 Usually, when one runs the npm run build command, tsc (typescript compiler) is executed and it transpiles typescript code into javascript.通常,当运行npm run build命令时,会执行tsc (打字稿编译器)并将打字稿代码转换为 javascript。 You don't need all of your dev dependencies included in the transpiled code.您不需要包含在转译代码中的所有开发依赖项。 Default location for transpiled code is usually dist folder.转译代码的默认位置通常是dist文件夹。 If your application entry point was src/index.ts , then after build command you should call node dist/index.js to run the server.如果您的应用程序入口点是src/index.ts ,那么在构建命令之后您应该调用node dist/index.js来运行服务器。 Now, everything you need to package your app for production is your package.json and transpiled code in dist folder.现在,打包生产应用程序所需的一切就是package.jsondist文件夹中的转译代码。 Install prod dependencies and run node dist/index.js and your server should run successfully.安装 prod 依赖项并运行node dist/index.js ,你的服务器应该会成功运行。 Of course, you should consider configuring PM2 or similar process manager for your production code.当然,您应该考虑为您的生产代码配置PM2或类似的流程管理器。

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

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