Today I create new project for my sample project. so, I write this post for future and this blog will use English language (I lazy write to Thai LoL)
To initialize a TypeScript project using pnpm, follow these steps:
- Create a new project directory and navigate into it:
mkdir my-ts-projectcd
cd my-ts-project- Initialize a pnpm project: This creates a
package.jsonfile.
pnpm init- Install TypeScript and Node.js type definitions:
pnpm add -D typescript @types/node ts-node- Initialize TypeScript configuration: This creates a
tsconfig.jsonfile.
pnpm tsc --initYou may want to adjust the compilerOptions in tsconfig.json according to your project's needs (e.g., target, module, outDir).
- Create a source directory and an entry file:
mkdir src
touch src/index.ts- Add a basic script to
src/index.ts:
TypeScript
// src/index.ts
console.log("Hello from TypeScript with pnpm!");- Add a script to
package.jsonto run your TypeScript code:
You can use ts-node for development to run TypeScript directly without compiling, or set up a build script for production code
// package.json
{
"name": "my-ts-project",
"version": "1.0.0",
"description": "",
"main": "dist/index.js",
"scripts": {
"start": "node src/index.js",
"build": "tsc",
"dev": "ts-node src/index.ts"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"@types/node": "^20.x.x",
"typescript": "^5.x.x",
"ts-node": "^10.x.x" // Add ts-node for dev script
}
}Remember to install ts-node if you use the dev script: pnpm add -D ts-node. Run your project code
pnpm run devOr, to build and then run code
pnpm run build
pnpm run start