Unitech / pm2

Node.js Production Process Manager with a built-in Load Balancer.
https://pm2.keymetrics.io/docs/usage/quick-start/
Other
41.54k stars 2.62k forks source link

How to use tsconfig-paths with pm2? #4413

Closed charleylla closed 4 years ago

charleylla commented 5 years ago

I'm trying to deploy my typescript project with pm2, but the argument seems not working correctly.

Here is ecosystem.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

When I run pm2 start ecosystem.config.yml and check the logs then:

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)

And my tsconfig.json is:

{
  "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"]
}

It seems that the paths alias are not working properly. It works well without pm2(this is a nestjs project):

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

Can anyone help me with this problem? Thanks.

llwwbb commented 5 years ago

pm2 install typescript pm2 start xxx.ts

stale[bot] commented 4 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.