3

我正在尝试使用 pm2 部署我的 typescript 项目,但该参数似乎无法正常工作。

这是生态系统.config.yml:

apps:
  - name: 'node-app'
    script: './src/main.ts'
    interpreter: './node_modules/.bin/ts-node'
    # they dont't work correctly below.
    interpreter_args: '-r tsconfig-paths/register'
    # args: '-T -r tsconfig-paths/register ./src/index.ts',
    # args: '-r ./node_modules/tsconfig-paths/register ./src/main.ts'
    # interpreter_args: ['--require=tsconfig-paths/register']
    # interpreter_args: '-P . -r ./node_modules/tsconfig-paths/register'
    kill_timeout: 10000
    instances: max
    exec_mode: cluster
    env:
      NODE_ENV: development

当我运行 pm2 start Ecosystem.config.yml 并检查日志时:

0|node-app | Error: Cannot find module '~framework/common/frame-util'
0|node-app |     at Function.Module._resolveFilename (internal/modules/cjs/loader.js:581:15)
0|node-app |     at Function.Module._load (internal/modules/cjs/loader.js:507:25)
0|node-app |     at Module.require (internal/modules/cjs/loader.js:637:17)
0|node-app |     at require (internal/modules/cjs/helpers.js:20:18)
0|node-app |     at Object.<anonymous> (/Users/zhaolin/__CODES__/__PERSONAL__/charley-nest-starter/script/ci/src/solution/gateway/auth/auth.service.ts:5:1)
0|node-app |     at Module._compile (internal/modules/cjs/loader.js:689:30)
0|node-app |     at Module.m._compile (/Users/zhaolin/.nvm/versions/node/v10.9.0/lib/node_modules/pm2/node_modules/ts-node/src/index.ts:473:23)
0|node-app |     at Module._extensions..js (internal/modules/cjs/loader.js:700:10)
0|node-app |     at Object.require.extensions.(anonymous function) [as .ts] (/Users/zhaolin/.nvm/versions/node/v10.9.0/lib/node_modules/pm2/node_modules/ts-node/src/index.ts:476:12)
0|node-app |     at Module.load (internal/modules/cjs/loader.js:599:32)

我的 tsconfig.json 是:

{
  "compilerOptions": {
    "module": "commonjs",
    "declaration": true,
    "removeComments": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "target": "es2017",
    "sourceMap": true,
    "outDir": "./dist",
    "baseUrl": "./",
    "incremental": true,
    "rootDir": ".",
    "paths": {
      "~framework/*":["src/framework/*"],
      "~solution/*":["src/solution/*"],
      "~business/*":["src/solution/business/*"],
      "~dto/*":["src/solution/dto/*"],
      "~entities/*":["src/solution/entities/*"],
      "~/*":["src/*"]
    }
  },
  "exclude": ["node_modules", "dist"]
}

路径别名似乎无法正常工作。它在没有 pm2 的情况下运行良好(这是一个 nestjs 项目):

...
    "start": "rimraf dist && cross-env NODE_ENV=development ts-node -r tsconfig-paths/register src/main.ts",
...

谁能帮我解决这个问题?谢谢。

4

1 回答 1

1

就像 Aleksandr 所说,您可以先使用 构建项目tsc,然后使用node,而不是ts-node.

例子:

  1. 在您的脚本运行器中(在 package.json 中),您可以定义:
  "build": "tsc",
  "runBuild": "pm2 start ecosystem.yml --only node-app"
  1. 在你的ecosystem.yml你可以定义:
apps:
  - name: 'node-app'
    script: './dist/src/main.js'
    node_args: '-r ts-node/register -r tsconfig-paths/register'
    kill_timeout: 10000
    instances: max
    exec_mode: cluster
    env:
      NODE_ENV: development
  1. 跑步:
$ npm run build
$ npm run runBuild

希望有帮助

于 2020-03-31T03:29:33.937 回答