The project demonstrates how to set up and debug TypeScript app inside Docker container with VS Code.
We use:
- Docker (Docker Compose)
- Node.js (Express.js) app
- TypeScript Compiler
- VS Code
ts-node
andnodemon
packages
-
If you encounter ant problems with TypeScript types (e.g. Express is
any
), here are all workarounds you can try: DefinitelyTyped/DefinitelyTyped#53397Currently I resolved the typing problem with Yarn: use Yarn instead of NPM:
- delete
node_modules
and all lock files - reinstall them by issuing
yarn install
- copy-paste the dependencies below, I've tested their versions, they're compatible
- replace
RUN npm install
withRUN yarn install
inDockerfile
{ "name": "typescript-dockerized-project", "version": "1.0.0", "description": "", "main": "src/index.js", "scripts": { "start": "nodemon", "test": "echo \"Error: no test specified\" && exit 1" }, "keywords": [], "author": "", "license": "ISC", "devDependencies": { "@types/express": "^4.17.2", "@types/express-serve-static-core": "^4.17.21", "@types/node": "^20.10.5", "nodemon": "^3.0.2", "ts-node": "^10.9.2", "typescript": "^5.3.3" }, "dependencies": { "express": "^4.17.2", "express-serve-static-core": "^0.1.1" }, "resolutions": { "@types/express-serve-static-core": "4.17.21", "@types/express": "4.17.2" } }
- delete
-
If you experience an error concerned with types on Request object of Express, do this: DefinitelyTyped/DefinitelyTyped#62263 .
In short, just issue this command:
npm i --save-dev @types/express-serve-static-core@4.17.28
- this fixes types, cause the latest version ofexpress-serve-static-core
has breaking changes or wrong types, I don't know, but something is wrong with it.
You can fine-tune configuration through options in tsconfig.json
and start
script in package.json
.
package.json
:
...
"start": "nodemon --watch ./src/**/* -e ts,json --exec TS_NODE_PROJECT=tsconfig.json node --inspect=0.0.0.0:9229 -r ts-node/register ./src/index.ts"
...
-
--watch ./src/**/*
tellsnodemon
to watch for onlysrc
folder -
--e ts,json
tellsnodemon
to watch only for the files with these extensions -
--exec TS_NODE_PROJECT=tsconfig.json node --inspect=0.0.0.0:9229 -r ts-node/register ./src/index.ts
tellsnodemon
to startnode
and pass itTS_NODE_PROJECT
environment variable which specifies the location and name of TypeScript Compiler config file (this variable is used by TypeScript Compiler).-r
flag is a short command-line variant ofrequire()
, it preloads the specified module at startup, in our case we preloadts-node/register
. Finally we pass tonode
process the name of the main app's file -index.ts
. -
If you need to debug the script from the very first line, change
... node --inspect=0.0.0.0:9229 ...
to... node --inspect-brk=0.0.0.0:9229 ...
Note the volume
option in dcoker-compose.yml
: we mount tsconfig.json
(to tell TSC how to compile .ts
files inside container) and src
folder (containing all source .ts
-files )
.vscode/launch.json
(pay attention to sourceMapPathOverrides
option and make sure you provided the correct value, otherwise the debugger won't stop on breakpoints)