Skip to content

Commit

Permalink
fix(init): TypeScript code is not being recompiled automatically (#11470
Browse files Browse the repository at this point in the history
)

The use of `npx ts-node` to run the app in the TypeScript init templates is intended to make sure the source code gets transpiled before every synth without the user needing to `npm run build` explicitly. I have had reports on the Hello World dev guide topic that an `npm run build` is still needed, even though it shouldn't be. I was able to reproduce this issue today (was not able to do so previously).

The problem is that, by default, `ts-node` prefers resolving imports to JavaScript files. If a JavaScript file exists, as it would be after the first build, it is simply imported as-is. The corresponding TypeScript module is not transpiled. Therefore changes you make to the TS code are never picked up and stale JS code is used.

The flag `--prefer-ts-exts` solves this issue. It causes module imports to prefer importing TypeScript files, which means that the modules will get recompiled if necessary.

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
Jerry Kindall authored Nov 17, 2020
1 parent 989d15f commit 9843e71
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"app": "npx ts-node bin/%name%.ts"
"app": "npx ts-node --prefer-ts-exts bin/%name%.ts"
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"jest": "^26.4.2",
"ts-jest": "^26.2.0",
"aws-cdk": "%cdk-version%",
"ts-node": "^8.1.0",
"ts-node": "^8.3.0",
"typescript": "~3.9.7"
},
"dependencies": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"app": "npx ts-node bin/%name%.ts"
"app": "npx ts-node --prefer-ts-exts bin/%name%.ts"
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"@types/node": "10.17.27",
"jest": "^26.4.2",
"ts-jest": "^26.2.0",
"ts-node": "^8.1.0",
"ts-node": "^8.3.0",
"typescript": "~3.9.7"
},
"dependencies": {
Expand Down

0 comments on commit 9843e71

Please sign in to comment.