Development Environment
This runtime leads to extremely high memory consumption and server overload, so it should not be used in production.
https://pm2.io/docs/runtime/integration/transpilers/
We don’t recommend using this in production as it slows down your app.
"scripts": {
"pm2": "NODE_ENV=production pm2 start server.ts --watch"
}
Production Environment
You can transpile TypeScript to JavaScript with a separate command and run it using npm run pm2
(or npm run pm2:staging
if you have a staging environment).
The commands npm run prod
and npm run staging
should only be used locally when you need to work with production and staging environments.
"scripts": {
"pm2": "NODE_ENV=production pm2 start build/server.js --watch -i max",
"pm2:staging": "NODE_ENV=staging pm2 start build/server.js --watch -i max",
"prod": "NODE_ENV=production node build/server.js",
"staging": "NODE_ENV=staging node build/server.js",
"dev": "HTTPS=true NODE_ENV=development ts-node-dev --inspect --respawn src/server.ts",
"build": "rimraf build && tsc -p tsconfig.json",
"test": "NODE_ENV=test nyc ./node_modules/.bin/mocha --require ts-node/register ./src/test/**/**/**/**/*.test.ts",
}
Your tsconfig.json
compilerOptions
should look something like this:
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"lib": ["es2015", "dom"],
"sourceMap": true,
"outDir": "./build",
"strict": true,
"strictPropertyInitialization": false,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
"moduleResolution": "node",
"types": ["reflect-metadata"],
"esModuleInterop": true,
"inlineSources": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"skipLibCheck": true
}
PM2 can generate startup scripts and configure them to keep your process list intact across expected (and unexpected) machine restarts. This is important to keep automated. https://pm2.keymetrics.io/docs/usage/startup/
pm2 unstartup
pm2 startup
pm2 save
pm2 start ts-node -- -P tsconfig.server.json ./server/index.ts
– Celestyna