简体   繁体   English

使用CLI package.json脚本时生产部署失败

[英]Production deployment failing when using CLI package.json scripts

I have created a basic application through the CLI tool (v6.3.0) and have reached the point where I was ready to push to a production server and came across an issue. 我已经通过CLI工具(v6.3.0)创建了一个基本应用程序,并且已经达到我准备推送到生产服务器并遇到问题的程度。

I am deploying using Shipit (which is probably not relevant) and part of this process is to install the npm dependencies (with the flag --production ). 我使用Shipit部署(可能不相关),此过程的一部分是安装npm依赖项(带有标志--production )。

Everything deploys without a hitch, until my deployment runs the final command npm run start:prod (to start the Nest application on the node process). 一切都顺利部署,直到我的部署运行最终命令npm run start:prod (在节点进程上启动Nest应用程序)。 Which executes the following steps: 其中执行以下步骤:

  1. Deletes the contents of the dist directory rimraf dist && npm run build ; 删除dist目录的内容rimraf dist && npm run build ;
  2. Tries to rebuild the contents of the dist directory tsc -p tsconfig.build.json ; 尝试重建dist目录的内容tsc -p tsconfig.build.json ;
  3. Runs node dist/main.js ; 运行node dist/main.js ;

The problem with all this, is that the TypeScript package that I believe provides the tsc command is a devDependency not a dependency that is installed during npm install with the --production flag. 所有这一切的问题是,我认为提供tsc命令的TypeScript包是devDependency而不是在npm安装期间使用--production标志安装的依赖项。

Is this a bug, or am I completely missing the point of the npm run start:prod command. 这是一个错误,还是我完全npm run start:prodnpm run start:prod命令的要点。 I appreciate that installing the TypeScript package globally may resolve the issue, although I wasn't sure if that was the intention, or assumption? 我感谢全局安装TypeScript包可能会解决问题,虽然我不确定这是出于意图还是假设?

If you deploy NestJS to production, are you utilising some other strategy? 如果将NestJS部署到生产环境中,您是否正在使用其他策略?

In server-side applications the distinction between dependencies and devDependencies is not as important, since you're not shipping your dependencies and hence a the size of your application is not as critical. 在服务器端应用程序中, dependenciesdevDependencies之间的区别并不重要,因为您没有提供依赖项,因此应用程序的大小并不重要。

However, when you deploy to the cloud you might want to save space. 但是,当您部署到云时,您可能希望节省空间。 TypeScript is not needed to run your application once it is compiled. 编译后运行应用程序不需要TypeScript。 And you don't need to recompile your application when you simply want to restart it. 当您只想重新启动应用程序时,无需重新编译应用程序。 So, what you could do instead: 那么,你可以做什么呢?

Remove the prestart:prod script. 删除prestart:prod脚本。

Deployment routine: 部署例程:

  1. Install all dependencies with npm install 使用npm install安装所有依赖项
  2. Run npm run build 运行npm run build
  3. Prune your dev dependencies with npm prune --production 使用npm prune --production修剪你的dev依赖项
  4. Run npm run start:prod 运行npm run start:prod

This is pretty much what happens when I deploy to heroku. 这几乎是我部署到heroku时发生的事情。

Alternatively, you can of course declare typescript under dependencies instead of devDependencies . 或者,你当然可以声明typescriptdependencies ,而不是devDependencies

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

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