In this post, we will organize our project by moving our source files into a src
folder and configuring the TypeScript compiler to output the compiled JavaScript files into a out
folder. This separation of source and compiled files will make our project easier to manage as it grows.
src
FolderTo start, let’s create a dedicated src
folder at the root of our project. This will be the home for all our TypeScript source code files. Move the existing server.ts
file into this new folder. If you already have a compiled server.js
file in the root directory (generated from the original server.ts
), please delete it to avoid any conflicts.
Now, we need to update our tsconfig.json
file to indicate where the source files are and where to output the compiled files. Update the rootDir
and outDir
options in tsconfig.json
as follows:
{
"compilerOptions": {
"rootDir": "./src",
"outDir": "./out",
// other options...
},
// other settings...
}
The rootDir
option tells TypeScript where to find the source files, and the outDir
option tells it where to output the compiled files.
start
ScriptFinally, we need to update our start
script in package.json
to run the server from the compiled file in the out
folder:
"scripts": {
- "start": "tsc && node server.js",
+ "start": "tsc && node out/server.js",
}
Now, when you run npm run start
, TypeScript will compile the source files from the src
folder and output the compiled JavaScript files into the out
folder. The start
script will then run the server from the compiled server.js
file in the out
folder.
By organizing our project this way, we keep our source and compiled files separate, making it easier to manage our project and prepare it for deployment. In the next post, we’ll explore how to use VSCode as an integrated development environment (IDE) for our project.