How to use tsconfig-paths with pm2?
See original GitHub issueI’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.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:2
- Comments:7
Top Results From Across the Web
Run typescript build with tsconfig-paths using pm2
I am trying to run the build (.js files) of typescript with the tsconfig-paths in production, ...
Read more >tsconfig-paths - npm
Start using tsconfig-paths in your project by running `npm i tsconfig-paths`. There are 971 other projects in the npm registry using ...
Read more >Configuring PM2 in docker for Node.js | by ParkJaJay - Medium
Like we applied tsconfig-paths library in ts-node, we have to apply another library in node, which is module-alias. First, install module-alias.
Read more >Using transpilers with PM2
The easiest way to use transpilers with PM2 is to override the execution interpreter ( exec_interpreter ). Note that if this is changed,...
Read more >Transpilers | Integration | PM2 Documentation
This tutorial will show you how to use pm2 with transpilers. We highly don't recommend to use this in production as it slows...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
pm2 start bootstrap.js --node-args=“-r tsconfig-paths/register”
On my case it was a little bit more complex because I was pointing to JS in production (so my tsconfig.json needed to use baseUrl as ./dist/src and not ./src). Locally I’m running on TS, so the issue was only in prod.
To solve it…
my package.json
my pm2.json - I had to add a custom register-paths.js file here, on my node_args
register-path.js file
Hope it helps! It took me 2 hours to figure it out. 😃