- JavaScript Basics
- Node.js Environment
- Package Manager (npm/pnpm)
- Install
tsxdependencies:
npm install --save-dev tsx
- Run TypeScript code:
npm run tsx your-file.ts
- You also can run TypeScript code directly with tsx:
tsx your-file.ts
- Install typescript dependencies:
npm install --save-dev typescript
- Initiate a TypeScript project:
npx tsc --init
- Create a new
.tsfile in thesrc/folder. You can add and edit.tsfile insrc/folder.
mkdir src
touch src/index.ts
- Update the
package.jsonfile with a new script:
{
"scripts": {
"dev": "tsc && node ./build/index.js"
},
"devDependencies": {
"typescript": "^5.8.2"
}
}
- Configure the
tsconfig.jsonfile, add"outDir": "./build"to specify where the compiled JavaScript files should be placed. Also, set"rootDir": "./src"to indicate the root directory of your source files.
{
"compilerOptions": {
...
"outDir": "./build",
...
"rootDir": "./src"
...
}
}
- Happy Coding. If you want build and run the project, run the build script to compile TypeScript code into JavaScript:
npm run dev