This repository is a starter for setting up Typescript.
It provides the basic configuration and tools to write and run TypeScript code in a simple and efficient way.
# Install dependencies
npm install
# Start development server
npm run dev
# Build for production
npm run build
# Or build for production and start server
npm run start
- Install Visual Studio Code
- Use configuration files from
.vscode
folder:- Use the command palette (Ctrl+Shift+P) and select
Debug: Select and Start Debugging
to run one of the debug configurations defined inlaunch.json
.
- Use the command palette (Ctrl+Shift+P) and select
node --env-file=../../.env --watch --require tsconfig-paths/register --require ts-node/register src/main.ts
--env-file=../../.env
- loads environment variables from the.env
file located in the root directory of the project.--watch
- watches the file system for changes and restarts the server when a change is detected.--require tsconfig-paths/register
- requires thetsconfig-paths/register
package, which reads the paths and baseUrl options in your tsconfig.json file and redirects the imports at runtime.--require ts-node/register
- requires thets-node/register
package, which allows you to run TypeScript files directly without compiling them first.
In this project, we use ts-alias
and tsconfig-paths
to simplify the import paths in our TypeScript files (i.e. @/utils
instead of ../../utils
).
-
tsconfig-paths
is a package that reads the paths and baseUrl options in yourtsconfig.json
file and redirects the imports at runtime. This allows us to use shorter and more readable import paths, which are especially useful in a large codebase (e.g.@/utils
instead of../../utils
). -
ts-alias
is used to rewrite these paths in the compiled JavaScript files. When TypeScript compiles the code, it doesn't rewrite the import paths, so without ts-alias, the paths would be incorrect in the compiled code, leading to module resolution errors (e.g.Cannot find module '@/utils'
).
By using these two packages together, we can have both the convenience of shorter import paths during development and the assurance that the paths will be correct in the production build.