Closed opensas closed 4 years ago
I ran into this issue during porting some of this code for ts-patch. The problem is that tts walks up the node modules chain to look for ts-node. If it finds it, it does not call ts-node.register. The problem is, there are cases where ts-node can be loaded without having had register called.
In this case, during createProgram, when it tries to load your Typescript based transformer, it will fail because it cannot interpret the TS in the transformer.
It sounds like that's what's happening. I don't maintain tts, but you're welcome to give ts-patch a try. If you run into any problems, please don't hesitate to file an issue!
@opensas Can you please provide repro project. I have tried your example, and it works for me
Sure, I tried to reproduce a pretty minimal example here: https://gitlab.com/opensas/tts-node_test
package.json
{
"name": "tmp",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"ts-node": "ts-node -r tsconfig-paths/register src/start.ts",
"tts-node": "ts-node -C ttypescript src/start.ts"
},
"author": "",
"license": "ISC",
"devDependencies": {
"@types/express": "^4.17.2",
"ts-node": "^8.5.4",
"ts-transformer": "^0.1.5",
"ts-transformer-imports": "^0.4.3",
"tsconfig-paths": "^3.9.0",
"ttypescript": "^1.5.8",
"typescript": "^3.7.2"
},
"dependencies": {
"express": "^4.17.1"
}
}
tsconfig.json
{
"compilerOptions": {
"baseUrl": "./src",
// "paths": {
// "@*": ["./*"]
// },
"plugins": [
{ "transform": "ts-transformer-imports" }
],
"esModuleInterop": true
}
}
src/start.ts
import app from './app'
app.get('/ping', (req: any, res: any) => {
return res.send('ok!')
})
app.listen(8080, () =>
console.log('listening on localhost:8080/ping')
)
src/app.ts
import express from 'express'
export default express()
$ npm -v
6.11.3
$ node -v
v12.11.1
@opensas The problem lies within ts-transformer-imports
see this issues for more info
https://github.com/cevek/ttypescript/issues/50
https://github.com/cevek/ttypescript/issues/43
so, just try another path mapper transformer like https://github.com/zerkalica/zerollup/tree/master/packages/ts-transform-paths
I can confirm that it works ok, I just did the following:
npm i -D @zerollup/ts-transform-paths
and then
tsconfig.json
{
"compilerOptions": {
"outDir": "build",
"baseUrl": "src",
"paths": {
"@*": ["./*"]
},
// "plugins": [
// { "transform": "ts-transformer-imports" }
// ],
"plugins": [
{ "transform": "@zerollup/ts-transform-paths", "exclude": ["*"] }
],
"esModuleInterop": true
}
}
and now ts-node -C ttypescript src/start.ts
works ok
I was using ts-node with tsconfig-paths like this:
and everything worked fine. Then I tried to use it with ttypescript to get rid of the tsconfig-paths dependency, like it's shown here: https://github.com/cevek/ttypescript#ts-node:
But when I try it I get the following error:
This is the file app.ts
I'm using ttsc to run
npm run prod
and they work ok.For the moment I'm keeping tsconfig-paths, but I'd like to get rid of it
thanks a lot