我有一个用 Typescript 编写的应用程序,它与 PM2 一起运行。目前我编译为 JavaScript,然后使用 PM2 启动应用程序。我的 ecosystem.config.js
文件如下所示:
module.exports = {
apps: [
{
name: 'My Application',
script: './dist/server/index.js',
env_qa: {
PORT: 3001,
NODE_ENV: 'production',
},
env_production: {
PORT: 3000,
NODE_ENV: 'production',
},
},
],
};
我使用以下命令运行它:
pm2 stop ecosystem.config.js --env qa
开发时,我只运行
ts-node server
而不是编译和使用 PM2。我最近读到 ts-node 具有“transpileOnly”或“快速”模式,这意味着它可以在生产中使用。首先,我想知道这是否可以在生产环境中使用。其次,我如何仍然使用 PM2 来启动我的应用程序但使用 ts-node?
最佳答案
开发环境
运行时会导致极高的内存消耗和服务器过载,最终无法用于生产,链接https://pm2.io/docs/runtime/integration/transpilers/
"scripts": {
"pm2": "NODE_ENV=production pm2 start server.ts --watch"
}
生产用途使用单独的命令将 typescript 转换为 javascript 并使用
npm run pm2
运行它和 npm run pm2:staging
如果你有一个暂存环境。命令
npm run prod
和 npm run staging
仅当我需要在本地使用生产和登台环境时才在本地使用。 "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",
"test": "NODE_ENV=test nyc ./node_modules/.bin/mocha --require ts-node/register ./src/test/**/**/**/**/*.test.ts",
"build": "rimraf build && tsc -p tsconfig.json"
}
tsconfig.json{
"compilerOptions": {
/* Basic Options */
"target": "es5" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017','ES2018' or 'ESNEXT'. */,
"module": "commonjs" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */,
"lib": [
"es2015", "dom"
] /* Specify library files to be included in the compilation. */,
// "allowJs": true, /* Allow javascript files to be compiled. */
// "checkJs": true, /* Report errors in .js files. */
// "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
// "declaration": true, /* Generates corresponding '.d.ts' file. */
// "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */
"sourceMap": true /* Generates corresponding '.map' file. */,
// "outFile": "./", /* Concatenate and emit output to single file. */
"outDir": "./build" /* Redirect output structure to the directory. */,
// "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
// "composite": true, /* Enable project compilation */
// "removeComments": true, /* Do not emit comments to output. */
// "noEmit": true, /* Do not emit outputs. */
// "importHelpers": true, /* Import emit helpers from 'tslib'. */
// "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */
// "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */
/* Strict Type-Checking Options */
"strict": true /* Enable all strict type-checking options. */,
// "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */
// "strictNullChecks": true, /* Enable strict null checks. */
// "strictFunctionTypes": true, /* Enable strict checking of function types. */
"strictPropertyInitialization": false, /* Enable strict checking of property initialization in classes. */
// "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
// "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */
/* Additional Checks */
"noUnusedLocals": true /* Report errors on unused locals. */,
"noUnusedParameters": true /* Report errors on unused parameters. */,
"noImplicitReturns": true /* Report error when not all code paths in function return a value. */,
"noFallthroughCasesInSwitch": true /* Report errors for fallthrough cases in switch statement. */,
/* Module Resolution Options */
"moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
// "baseUrl": "./", /* Base directory to resolve non-absolute module names. */
// "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
// "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */
// "typeRoots": [], /* List of folders to include type definitions from. */
"types": ["reflect-metadata"],
// "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
"esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */,
// "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */
/* Source Map Options */
// "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */
// "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
// "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */
"inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */
/* Experimental Options */
"experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */
"emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */
"skipLibCheck": true
}
}
PM2 可以生成启动脚本并对其进行配置,以使您的进程列表在预期或意外的机器重启时保持完整,这对于保持自动化很重要,链接 https://pm2.keymetrics.io/docs/usage/startup/pm2 unstartup
pm2 startup
pm2 save
关于javascript - 使用 PM2 运行 Typescript 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56566580/