问题
From what I have seen so far, nodejs apps get deployed either by creating a completely static bundle (e.g. angular), or by pulling the source from the repo and running npm install
and npm start
(usually seen when express/mongo are used).
However, I am developing an express/mongo app paired with typescript, and pulling from the repo would require to build the typescript (again) on the production server, which is ugly and also requires to have typescript and all @types/*
packages as production-dependencies. So I was wondering: what are the best practices? Is it a good idea to build & bundle all necessary (.js) files and push them to the server together with package.json, so I can still run npm install
? What would be a better way?
回答1:
You are correct that you don't want to be compiling typescript on a production server. Generally, you want to take care of all building and dependency management before you hit production. This means creating a build pipeline that outputs a standalone application for deployment.
A typical build pipeline might look like this:
npm install
→ lint
→ build
→ bundle *.js and node_modules
→ deploy
A setup like this isolates installing dependencies, linting, and building to the pipeline. Only *.js
files, an already installed node_modules
directory, and package.json
(if it contains the startup script) actually get deployed.
The build pipeline can be done manually, by running a build script and copying the output to the production server. However, a better way is to use one of the many build automation solutions that are available, such as Jenkins, Travis CI, or GitLab CI.
来源:https://stackoverflow.com/questions/57560850/continuous-deployment-with-node-express-mongo-app-and-typescript